iOS Dev Nugget 107 Auto-sizing UITableViewCells


Need to run a code review on your codebase? Hire me

One simple and extremely helpful improvement introduced in iOS 8 SDK is the auto-sizing of UITableViewCells. Prior to iOS 8, if you had a UITableView that has cells with varying heights, you'd have to implement UITableViewDelegate's -tableView:heightForRowAtIndexPath: and return a height based on -systemLayoutSizeFittingSize:, potentially caching the heights yourself.

In iOS 8, this becomes much easier if you do the following:

  1. Use Auto Layout for your UITableViewCell (subclass).
  2. Set UITableView.rowHeight = UITableViewAutomaticDimension
  3. Set UITableView.estimatedRowHeight or implement -tableView:estimatedHeightForRowAtIndexPath:

You don't (and shouldn't) implement -tableView:heightForRowAtIndexPath: anymore!

Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No



Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter


View archives of past issues