我有一个带有自定义UITableView
的UITableViewCell
。根据对象的属性显示/删除单元格中的某些子视图。我正在使用自动布局和xib。以下是相关代码:
if (![some condition]) {
[self.descriptionLabel setText:descText];
} else {
[self.descriptionLabel removeFromSuperview];
}
这是单元格的示意图
-----------------------------
topLabel
descriptionLabel
bottomLabel
-----------------------------
唯一可以删除的标签是描述标签。我有一个从
bottomLabel
到descriptionLabel
的约束,另一个是从bottomLabel
到topLabel
的约束,具有较低的优先级。删除descriptionLabel
时,bottomLabel
正确地假定具有较低的优先级约束。问题在于我假设是单元重用,当我向上/向下滚动并且已删除
descriptionLabel
的单元被重用时,不会重新添加。我尝试过的替代品:
descriptionLabel
与删除bottomLabel
相比,通过这种方式,它可以保持其框架,因此ojit_code不会向上移动。 我是否需要重新初始化标签并将其添加到视图中?还是有更好的方法来处理此用例?
最佳答案
当您隐藏descriptionLabel
时,应在更改约束后调用layoutIfNeeded
方法。在更改约束时,应更改其优先级。 bottomLabel
到topLabel
约束的优先级应该高,而bottomLabel
到descriptionLabel
约束的优先级应该低。这比删除并重新添加标签更好。
关于ios - 使用AutoLayout从UITableViewCell删除 subview ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32464370/