我有点沮丧。我有一个带有 UITableViewAutomaticDimension
单元格的 iOS 10 项目 UITableView
。我正确地设置了约束。
最初我在里面绘制我的细胞
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
一切正常,但后来我读了一篇文章:https://medium.com/ios-os-x-development/perfect-smooth-scrolling-in-uitableviews-fd609d5275a5#.4rnbc4vyg并将我的数据映射移动到
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
在此之后我的自动布局单元停止正常工作。
我的理解是在 cellForRowAt
之后但在 willDisplay
之前计算高度,所以当我在 willDisplay
中映射时,高度已经设置为我的手机,我需要重新加载它(这不好)。
谁能证明/解释这个?
更新:
发帖后发现这篇文章:https://tech.zalando.com/blog/proper-use-of-cellforrowatindexpath-and-willdisplaycell/
最佳答案
始终在 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
Apple 文档对 cellForRowAt 说:
另一方面,Asks the data source for a cell to insert in a particular location of the table view.
willDisplay 可用于在单元格被绘制以供查看之前覆盖单元格的某些属性。如果您不确定 willDisplay,我建议您在 cellForRowAt 中进行所有设置。
Apple 的文档是关于 willDisplay 的:
This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color.
有关 willDisplay 的更多信息,请参见 Apple 文档: tableView:willDisplayCell:forRowAtIndexPath:
编辑:阅读您提供的链接后 - 我重申,除非您确实有特定理由在 willDisplayCell 中设置您的单元格,否则只需在 中进行cellForRowAt。
关于ios - UITableView cellForRowAt 与 willDisplay,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42649231/