iphone - heightForRowAtIndexPath : was executed before cell is show?

标签 iphone ios cocoa-touch uitableview

比如tableView有100行,heightForRowAtIndexPath:在第100行显示之前就已经执行了。

为什么?为了计算tableView的contentSize

谢谢!

最佳答案

来自 Apple's documentation :

Every time a table view is displayed, it calls tableView:heightForRowAtIndexPath: on the delegate for each of its rows

是的,它在显示单元格之前被调用。如果您考虑一下,如果表格以相反的方式进行,它将创建一个具有标准高度的单元格,将内容放入其中,获取实际高度,然后调整单元格大小/移动单元格。那可不太好。

这可能会导致性能问题,因此您应该只在具有不同高度的单元格时使用它:

The method allows the delegate to specify rows with varying heights. If this method is implemented, the value it returns overrides the value specified for the rowHeight property of UITableView for the given row.

There are performance implications to using tableView:heightForRowAtIndexPath: instead of the rowHeight property. Every time a table view is displayed, it calls tableView:heightForRowAtIndexPath: on the delegate for each of its rows, which can result in a significant performance problem with table views having a large number of rows (approximately 1000 or more).

否则,如果所有单元格的高度都相同,请使用 rowHeight 属性,并且不要实现 tableView:heightForRowAtIndexPath:

关于iphone - heightForRowAtIndexPath : was executed before cell is show?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12262588/

相关文章:

iPhone SDK : AVAudioRecorder metering -- how to change peakPowerForChannel from decibel into percentage?

ios - mkmap查看用户位置

iphone - 如何自动播放音频文件?

iphone - View 和 View Controller

iphone - UITableView 与 UITableViewController

用户评论 : sslv3 alert certificate expired 时 iPhone 应用程序错误

没有 Storyboard或图像的 iOS 启动图像

ios - UITabBarItem 不更新图像

ios - 按下按钮时激活 TextField

ios - UILabel 将文本居中对齐