我有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
vs删除它,但是,这样就维持了它的框架,因此bottomLabel
不会向上移动。我是否必须重新初始化标签并将其添加到视图中?或者有更好的方法来处理这个用例吗?
答案 0 :(得分:1)
隐藏descriptionLabel
后,应在更改约束后调用layoutIfNeeded
方法。在更改约束时,您应该更改其优先级。 bottomLabel
到topLabel
约束的优先级应该很高,bottomLabel
到descriptionLabel
约束的优先级应该很低。这比删除和添加标签更好。