0

I want to create a custom cell with number of labels in it. It is easy to create custom cell if you know the number of fixed labels required. But what if you don't know how many labels are needed for a particular cell. I get values from API side which tells the number of labels needed. If there are more than 3 labels, I will have a "view more" button to expand the cell to show more labels.

Right now I can think to achieve this by creating 3 labels in custom cell. And in the cellForRowAtIndexPath, I can hide last two labels and change the cell frame according data given from API. But I still have to figure out about showing more labels after clicking "view more" button. So what is the best way to solve this challenge? Your little help will be appreciated.

Note: I am not using storyboard. I create every objects programmatically.

Hiren Prajapati
  • 717
  • 3
  • 10
  • 25

4 Answers4

1

You can always put a UICollectionView inside of a row of your UITableView. Each cell of your UICollectionView will have a UILabel. Update the dataSource for your UICollectionView depending on the number you receive from your API. This is an approach if you don't wish to show the "view more" button.

Rikh
  • 4,078
  • 3
  • 15
  • 35
  • 1
    Do you really think thats the best solution? I doubt it. It should be easy for this common challenge. Why to use UICollectionView just for unpredicted number of UILabel in a cell? – Hiren Prajapati Nov 05 '16 at 08:01
  • Well the collection view will automatically handle setting up the labels and not to mention their layout on its own. If you **have** to show the view more button, you can always subtract 3 from the `dataSource` for your `UICollectionView` and **then** create your `UICollectionView` on the button action. But this would require a bit of handling on your part so I personally would avoid the button. – Rikh Nov 05 '16 at 08:04
0

Setup your cell programmatically. At the time, you need the cell, you probably have the data already and know how to construct it.

Even if you don't know it upfront, you can change the contents of the cell while it is presented (i.e. in case of asynchronous loading).

Depending on your data, using something like a text view with formatted text might also do the trick, or a multiline label. If the number of labels has a low upper bound, you might get away with multiple labels right from the start and setting them to invisible or removing them if not needed. Another option then is to use different pre-configured cells, depending on the number of labels.

To do this completely in Interface Builder alone seems a rather tedious work.

Be careful reusing cells in this scenario - either use different identifiers for different configurations, update the internals very carefully, or don't reuse at all.

Eiko
  • 25,601
  • 15
  • 56
  • 71
0

If you have either 2 or 3 labels, there is a simple solution solution that consists of putting a third label at the bottom of the cell and if this label should exist set its height constraint to whatever it should be, if it shouldn't exist set the height to zero

Else, if I really understood what you wanted to do, I can't think of a simpler solution than having a table view in your cell, table view that displays as many labels as you want, and then your cell height should be equal to 2 times the height of your embedded table view cell. And the button will change the height of the cell to being as high as the whole table view embedded height.

Hope it helps :)

Stormsyders
  • 285
  • 2
  • 11
0

you can dynamically set the tableView cell height. As you said, you are getting the information from API that how many labels would be there in a tableView cell. You can put all these data in an array. then check below code.

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return ([[array objectAtIndex:indexPath.row] count] * yourLabelHeight);
}
Sambit Prakash
  • 341
  • 4
  • 15
  • That was not my problem. My problem is that I;m creating these unpredictable number of labels into cell in cellForRowAtIndexPath method. So these are local labels I'm adding into cell. But every time I scroll, all these labels overlap each other. I cannot not track these labels. – Hiren Prajapati Nov 05 '16 at 09:46