UITableView cellForRowAt 与 willDisplay

UITableView cellForRowAt vs willDisplay

我有点沮丧。我有一个 iOS 10 个项目 UITableView,其中包含 UITableViewAutomaticDimension 个单元格。我正确地设置了约束。

最初我将我的细胞映射到

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 中映射时,我的单元格已经设置了高度,我需要重新加载又一次(这不好)。

任何人都可以proof/explain这个?

更新:

发帖后发现这篇文章: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.

tableView(_:cellForRowAt:)

另一方面,

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:

编辑:在阅读了您提供的 link 后 - 我重申,除非您确实有特定的理由在 willDisplayCell[ 中设置您的单元格=34=],在cellForRowAt里做就行了。