Swift如何正确更新UITableViewCell约束(Swift how to proper update UITableViewCell constraint)

编程入门 行业动态 更新时间:2024-10-09 05:19:02
Swift如何正确更新UITableViewCell约束(Swift how to proper update UITableViewCell constraint)

我想知道我如何正确更新tableviewcell约束?

在我的应用程序中,我正在检查用户是否正在使用iphone 6/7或iphone 6/7 Plus并依赖于我必须更改一些约束。

我已经创建了UITableViewCell的子类,如下所示:

class MyTableViewCell: UITableViewCell { }

我应该在prepareForReuse()或其他方法中更改约束吗?

override func prepareForReuse() { super.prepareForReuse() myConstraint.constant = someNewVal }

I wonder how I proper update a tableviewcell constraint?

In my app I am checking if the user is using a iphone 6/7 or iphone 6/7 Plus and depening on that I have to change some constraints.

I have made a subclass of UITableViewCell like this:

class MyTableViewCell: UITableViewCell { }

Should I change constraints in prepareForReuse() or in another method?

override func prepareForReuse() { super.prepareForReuse() myConstraint.constant = someNewVal }

最满意答案

从关于prepareForReuse的文档:

如果UITableViewCell对象是可重用的 - 也就是说,它具有重用标识符 - 在从UITableView方法dequeueReusableCellWithIdentifier:返回对象之前调用此方法。 出于性能原因,您应该仅重置与内容无关的单元格属性,例如,alpha,编辑和选择状态。 tableView中的表视图委托:cellForRowAtIndexPath:应该在重用单元格时重置所有内容。 如果单元对象没有关联的重用标识符,则不会调用此方法。 如果重写此方法,则必须确保调用超类实现。

因此,您可以使用prepareForReuse ,但由于性能原因,最好使用tableView:cellForRowAtIndexPath: .

如果我理解了docs,那么prepareForReuse应该尽可能快,所以dequeueReusableCellWithIdentifier:也会很快。 因此,最好在另一个方法中更改约束( tableView:cellForRowAtIndexPath:

From the docs about prepareForReuse:

If a UITableViewCell object is reusable—that is, it has a reuse identifier—this method is invoked just before the object is returned from the UITableView method dequeueReusableCellWithIdentifier:. For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state. The table view's delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell. If the cell object does not have an associated reuse identifier, this method is not called. If you override this method, you must be sure to invoke the superclass implementation.

Therefore, you can use prepareForReuse, but due to performance reasons, it would be better to use tableView:cellForRowAtIndexPath: .

If I understood the docs, prepareForReuse should be as fast as possible so dequeueReusableCellWithIdentifier: would be fast as well. So, it would be better to change the constraints in another method (tableView:cellForRowAtIndexPath: )

更多推荐

本文发布于:2023-07-30 15:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338780.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何正确   UITableViewCell   Swift   constraint   update

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!