iOS 8 Autosizing Cells

The latest iOS release (8.0 at the time this was written!) added a new cool feature: autosizing cells. Meaning that we don’t really need to implement tableView:heightForRowAtIndexPath: anymore.

How can we enable this?. Super simple, just add the following snippet:

[cc lang=”objc”]
self.tableView.estimatedRowHeight = SomeConstant;
self.tableView.rowHeight = UITableViewAutomaticDimension;
[/cc]

Now, here’s the deal. iOS 7 still requires tableView:heightForRowAtIndexPath: to be implemented. And if you do implement it, iOS 8 will disregard the Automatic Dimension settings.

Solution?.. import objc/runtime.h, and place this hack in your UITableView’s delegate:

[cc lang=”objc”]
– (BOOL)respondsToSelector:(SEL)aSelector {
if (sel_isEqual(aSelector, @selector(tableView:estimatedHeightForRowAtIndexPath:)) ||
sel_isEqual(aSelector, @selector(tableView:heightForRowAtIndexPath:))) {
return false;
}
return [super respondsToSelector:aSelector];
}
[/cc]

YES. It’s a hack =)