estimatedHeightForRowAtIndexPath can in fact change the final “correct” height of a row?

青春壹個敷衍的年華 提交于 2019-12-02 19:35:34

Updated Answer

After further investigation, it turns out "it simply requires Auto Layout" is incorrect. It was my sample code that required Auto Layout. I made a slight modification in DynamicHeightCell and it now works with or without Auto Layout.

Original Answer

It simply requires Auto Layout. Not surprising, really, but should absolutely be documented.

Here is a working project demonstrating tableView:estimatedHeightForRowAtIndexPath: working correctly with Auto Layout. If you turn off Auto Layout in the storyboard, it behaves as you've described.

Estimated Row Height Demo

Kusal Shrestha
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { }

This delegate method is usually used when you have numerous numbers of dynamic cells, which provides the rough estimation of the cell.

For exmaple: If you have 100 cells with each height ranging from 200 to 300 (e.g 201, 207, 250, 299, 300...) you can estimate the cell height of about 250. i.e.

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 250;
}
  • Providing an estimate the height of rows can improve the user experience when loading the table view.
  • If this delegate not implemented, it might be expensive to calculate all their heights and so lead to a longer load time.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!