我想知道我如何正确更新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: )
更多推荐
发布评论