OGeek|极客世界-中国程序员成长平台

标题: ios - UITableView cellForRowAt vs willDisplay [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:00
标题: ios - UITableView cellForRowAt vs willDisplay

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



Best Answer-推荐答案


始终在 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 vs willDisplay,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42649231/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4