Skip to content

Instantly share code, notes, and snippets.

@brockboland
Last active August 29, 2015 14:00
Show Gist options
  • Save brockboland/11272776 to your computer and use it in GitHub Desktop.
Save brockboland/11272776 to your computer and use it in GitHub Desktop.
Auto-sized UITableViewCells. This has *terrible* performance: a table with ~15 rows was noticeably jittery. It might be OK for very small tables with only a couple cells.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
/**
* As long as your custom cell classes are laid out in Interface Builder and
* have Autolayout constraints to the top and bottom edges of the content view,
* the size of the cell can be computed based on how tall it needs to be.
*
* USE AT YOUR OWN RISK. This causes a noticable performance hit on a table of
* only about 15 cells. It might work OK on tables with only a few cells, but
* be sure to test it for your actual use case. I'm only sharing this on gist
* because I found it interesting.
*/
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
[cell layoutIfNeeded];
CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
if (size.height > 0) {
return size.height;
}
else {
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment