Last Updated: July 05, 2019
·
1.085K
· isuruj

Self Sizing UITableViewCells

In iOS 8, creating auto resizing UITableViewCells is really easy.

For this article, I'll take an example where you have a custom cell with just one UILabel.

  • Add constraints to the label. Make sure there are no auto layout errors or warnings. Remember not to add a height constraint because obviously if you fix the height of the label, it can't resize itself.

Picture

  • Another important thing to remember is to set the Lines property of the label to 0. You can do this from the IB itself.

  • Next add the following two lines of code to the viewDidLoad() of the UITableViewController. 44 is the default height of a cell. You can adjust it all you want.

tableView.estimatedRowHeight = 44
tableView.rowHeight = UITableViewAutomaticDimension

And that's it! Here's the result.

Picture