5

我有点沮丧。我有一个UITableView带有UITableViewAutomaticDimension单元格的 iOS 10 项目。我正确设置了约束。

最初我在里面映射我的细胞

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)

在此之后,我的自动布局单元停止正常工作。

我的理解是 at height 是在 after cellForRowAtbut before计算的willDisplay,所以当我映射willDisplay高度时,我的单元格已经设置好了,我需要重新加载它(这不好)。

任何人都可以证明/解释这一点吗?

更新:

发布一个问题后,我找到了这篇文章:https ://tech.zalando.com/blog/proper-use-of-cellforrowatindexpath-and-willdisplaycell/

4

1 回答 1

6

始终在里面设置你的单元格func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

苹果文档说cellForRowAt

要求数据源将单元格插入表格视图的特定位置。

tableView(_:cellForRowAt:)

另一方面, willDisplay可用于在将单元格绘制到视图之前覆盖单元格的某些属性。如果您不确定 willDisplay,我建议您在 cellForRowAt 中进行所有设置。

苹果的文档说 willDisplay:

此方法使委托有机会覆盖先前由表视图设置的基于状态的属性,例如选择和背景颜色。

有关 willDisplay 的更多信息,请参见 Apple 的文档: tableView:willDisplayCell:forRowAtIndexPath:

编辑:阅读您提供的链接后 - 我重申,除非您真的有特定理由在willDisplayCell中设置您的单元格,否则只需在cellForRowAt中进行。

于 2017-03-07T13:20:26.087 回答