Self Sizing UITableViewCells
In iOS 8, creating auto resizing UITableViewCell
s 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.
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 theUITableViewController
. 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.
Written by Isuru Nanayakkara
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ios
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#