1

I have a TableViewController which contains 2 sections: Section 1 - is a cell which is loaded from a xib file. This cell just contains a TextView. Section 2 - Contains multiple cells populated from an Array.

The section 1 only exists if the master (previous) UITableView cell you select contains a certain piece of data.

All of the above works as expected, below is the parent view. The list of items come from a database, some items have a description, and some do not. For example below this image, you'll see the view is 'Classic Starters' is selected. Then below that, you'll see the view if 'Stir Frys' is selected. Stri Frys contains a description:

enter image description here

enter image description here

enter image description here

Now, what I want is, the description cell which is shows on the Stir Frys page, to automatically grow depending on the length of the text inside it. So if a description is 10 lines long, it will grow to show all 10 lines.

Does this have to be done programmatically, or is their a feature in XCode I'm missing ?

JamMan9
  • 706
  • 2
  • 9
  • 22
  • 1
    Give UITableViewAutomaticDimension in heightForRowAt delegate method for the cell containing description – Anuraj Jan 24 '18 at 13:27

2 Answers2

3

You can use UITableViewAutomaticDimension.

1) Set properties estimatedRowHeight and rowHeight of your tableview, in viewDidLoad for example. Like this :

    self.tableView.estimatedRowHeight = 50
    self.tableView.rowHeight = UITableViewAutomaticDimension

2) Return UITableViewAutomaticDimension in your heightForRowAt delegate method :

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
Jay
  • 91
  • 5
  • Won't this set all the cells to that height ? Also what about making it dynamic, each description might be different for example – JamMan9 Jan 24 '18 at 13:37
  • The property estimatedRowHeight only improve tableView loading performance, I put 50 arbitrarily but that's not really important. Here, the important thing is UITableViewAutomaticDimension, you must return it for your description cells. – Jay Jan 24 '18 at 13:45
  • Ah right, I'll try implement this now then. I'll accept your answer. Thanks – JamMan9 Jan 24 '18 at 14:48
1

First you need to do is to set estimatedRowSize to a value that best estimates most common size, and rowSize to UITableViewAutomaticDimension:

self.tableView.estimatedRowHeight = 44
self.tableView.rowHeight = UITableViewAutomaticDimension

In your case, since except the first one all the other cells are supposed to be the same, you can use the height of the rest of the cells as the estimatedRowHeight.

You don't have to implement heightForRowAt at all.

The second step you need to do is to setup proper constraints on the cells. That means you have to constrain the contents of the cell to the left, right, top and bottom of the cell, so that when contents grow, the cell will need to grow, too. Common mistake is to forget to constrain bottom, so then the cell does not grow and the contents leak through the bottom of the cell.

Third, since your dynamic cell contains UITextView, you need to make sure that it will grow with its text. That is not automatic. To achieve that, based on this answer, this should suffice (in the cell):

textView.isScrollEnabled = false

If you are using storyboard, just uncheck scroll enabled.

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90