ios - UITableView cellForRowAt 与 willDisplay

标签 ios uitableview autolayout uitableviewautomaticdimension

我有点沮丧。我有一个带有 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.

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:

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

关于ios - UITableView cellForRowAt 与 willDisplay,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42649231/

相关文章:

swift - 在 UICollectionView 中使用自动布局绘制圆圈的正确时机

ios - 如何将数据从 UI Table View 传递到 Table View Cell 内的 UI Collection View?

ios - 如何为非基于文本的内容创建 UIMenuController?

ios - 向下滚动并从 UITableViewController 返回时,导航栏标题变小

ios - 计数函数在 Swift 2 中不起作用

ios - 如何将 UICollectionView 项目的大小调整为具有 Collection View 高度的正方形大小?

ios - 使用 Storyboard 中的自动布局更改横向 View 框架

ios - 使用相机和相册

iOS Swift 5 API 响应不是 JSON,它有分号?

ios - 此类不符合键标签的键值编码