菜鸟教程小白 发表于 2022-12-13 15:00:56

ios - UITableView cellForRowAt vs willDisplay


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

<p>最初我是在里面映射我的细胞</p>

<p><code>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell</code></p>

<p>一切正常,但后来我读了一篇文章:<a href="https://medium.com/ios-os-x-development/perfect-smooth-scrolling-in-uitableviews-fd609d5275a5#.4rnbc4vyg" rel="noreferrer noopener nofollow">https://medium.com/ios-os-x-development/perfect-smooth-scrolling-in-uitableviews-fd609d5275a5#.4rnbc4vyg</a>并将我的数据映射移动到 </p>

<p><code>func tableView(_tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)</code></p>

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

<p>我的理解是在 <code>cellForRowAt</code> 之后但在 <code>willDisplay</code> 之前计算高度,所以当我在 <code>willDisplay</code> 中映射时,高度已经设置为我的手机,我需要重新加载它(这不好)。</p>

<p>任何人都可以证明/解释这一点?</p>

<p><strong>更新:</strong></p>

<p>发了一个问题后,发现这篇文章:<a href="https://tech.zalando.com/blog/proper-use-of-cellforrowatindexpath-and-willdisplaycell/" rel="noreferrer noopener nofollow">https://tech.zalando.com/blog/proper-use-of-cellforrowatindexpath-and-willdisplaycell/</a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>始终在 <code>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell</code> </p> 中设置你的单元格

<p>Apple 文档中提到了 <strong>cellForRowAt</strong>:</p>

<blockquote>
<p>Asks the data source for a cell to insert in a particular location of the table view.</p>
</blockquote>

<p> <a href="https://developer.apple.com/reference/uikit/uitableviewdatasource/1614861-tableview" rel="noreferrer noopener nofollow">tableView(_:cellForRowAt:)</a> </p>

<p><strong>willDisplay</strong> 可用于在单元格被绘制以供查看之前覆盖单元格的某些属性。如果您不确定 willDisplay,我建议您在 cellForRowAt 中完成所有设置。</p>

<p>Apple 的文档中提到了 willDisplay:</p>

<blockquote>
<p>This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. </p>
</blockquote>

<p>关于 willDisplay 的更多信息在 Apple 的文档中:
<a href="https://developer.apple.com/reference/uikit/uitableviewdelegate/1614883-tableview?language=objc" rel="noreferrer noopener nofollow">tableView:willDisplayCell:forRowAtIndexPath:</a> </p>

<p><strong>编辑</strong>:阅读您提供的链接后 - 我重申,除非您确实有特定理由在 <strong>willDisplayCell</strong> 中设置您的单元格,否则请在 <strong> 中进行cellForRowAt</strong>。 </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UITableView cellForRowAt vs willDisplay,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42649231/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42649231/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UITableView cellForRowAt vs willDisplay