I want to leverage Auto Layout taking care of my cell heights automatically in iOS 8, but I hate how finicky estimated row heights make scrolling (jumpy, scroll position unpredictable, etc.).
Is there a way to not use estimation so that the table view precalculates all row heights like it did pre iOS 7 (not too many cells so not a performance issue) but uses iOS 8's auto calculating for heights?
Edit:
There is no way to use dynamic sizing without using this property. estimatedRowHeight
tells the system to use a dynamic size, assuming you used Auto layout in you cell.
Watch WWDC 2014 Video 226.
//
Very simple - create your prototype cell in the Storyboard, add constraints to the superview top/bottom (aka the cell content view), meaning your subviews have top/bottom constraints that will "push" the cell size when necessary.
Make the that both the TableView and the Cell height are set to Default. If that's problem (auto layout required more height than the default 44 pts) then make sure to do it in code:
tableView.rowHeight = UITableViewAutomaticDimension
Don't forget to set the estimatedRowHeight
to tell the system you want dynamic sizing enabled.
Auto-sizing requires that you provide estimated row heights, but you can precalculate row heights using auto layout.
To do that you'll need a "dummy" cell filled with data for the cell which size you're trying to calculate. And then you can use auto-layout to do the work for you:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
static YourCell *dummy = nil;
static NSLayoutConstraint *widthConstraint = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
UINib *nib = [UINib nibWithNibName:@"YourCell" bundle:nil];
dummy = [[nib instantiateWithOwner:nil options:nil] firstObject];
widthConstraint = [NSLayoutConstraint
constraintWithItem:dummy.contentView attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual toItem:nil attribute:
NSLayoutAttributeWidth multiplier:1.0 constant:320.0];
widthConstraint.priority = 750; //Good idea to lower priority of this constraint so it doesn't interfere with whatever table view sets internally
[dummy addConstraint:widthConstraint];
});
//account for current table view width
widthConstraint.constant = tableView.bounds.width;
//...
//Configure dummy cell here
//...
CGSize size = [dummy systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height;
}
you can use either
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
or
_activityTable.estimatedRowHeight = [CompanyEventCell cellHeight];