How does estimatedRowHeight work?

Vaibhav Singh
2 min readAug 14, 2021

--

This is a short article about how estimatedRowHeight helps us out in implementing self-sizing tableview cells and also provides performance enhancements!

To enable self-sizing table view cells we need set the row height to UITableViewAutomaticDimension and provide a non-negative value for estimatedRowHeight. This tells the tableview to set the height of the cell based on its intrinsic content size. For this, to work we will need to provide appropriate constraints and make sure the subViews of the cell has its top and bottom edges pinned to the contentView of the cell (not the cell itself).

To determine the actual row height, the tableview asks each cell what the height of its contentView needs to be based on a given width of the tableview. Just going back to the point above, this is why our constraints need to be on the contentView instead of the cell itself.

Pre iOS 11 the default value for estimatedRowHeight was 0 and there was no height estimation by default. Since iOS 11 this value is 44, the height is estimated as numberOfCell * 44 by default.

Providing a non-negative value for estimatedRowHeight can help improve the performance of loading the tableview, If we have variable row heights, it will be expensive to calculate the height for all rows when the tableview loads. But when we provide a value for this variable, it defers some of that calculation from loading time to scrolling time hence improving the loading performance. We should try and make estimated row height as accurate as possible, the system calculates height using these so the more accurate they are, the more seamless the user experience is. estimatedRowHeight is also used to determine how many cells the tableview needs to create before it needs to display them to endure they are ready.

estimatedRowHeight is also used to correctly size the scrollView indicator, even if we are off in this estimate the tableview does a decent job of calculating the scrollView indicator’s height for us. But if we happen to have a cell with largely varying width we might see the scrollView indicator jumping when we scroll through the tableview. In this case, we should implement tableView:estimatedHeightForRowAtIndexPath to do some minimal calculations to come up with a better estimate.

--

--

Vaibhav Singh
Vaibhav Singh

Written by Vaibhav Singh

iOS Engineer @ Pulselive Leading development of the Premier League App!

Responses (1)