使用AutoLayout从UITableViewCell中删除子视图

时间:2015-09-08 18:02:33

标签: ios objective-c uitableview autolayout

我有UITableView个自定义UITableViewCell。根据对象的属性显示/删除单元格中的某些子视图。我正在使用自动布局和xib。这是相关的代码:

if (![some condition]) {
    [self.descriptionLabel setText:descText];
} else {
    [self.descriptionLabel removeFromSuperview];
}

这是细胞的粗略图表

-----------------------------
    topLabel

    descriptionLabel

    bottomLabel
-----------------------------

唯一可以删除的标签是描述标签。我有一个约束,从bottomLabeldescriptionLabel,另一个约束从bottomLabeltopLabel,优先级较低。删除descriptionLabel后,bottomLabel会正确地假定优先级较低的约束。

问题出现在我假设的细胞重用中,当我向上/向下滚动并且删除了已删除descriptionLabel的单元格时,它不会被重新添加。

替代我尝试过:

  • 隐藏descriptionLabel vs删除它,但是,这样就维持了它的框架,因此bottomLabel不会向上移动。

我是否必须重新初始化标签并将其添加到视图中?或者有更好的方法来处理这个用例吗?

1 个答案:

答案 0 :(得分:1)

隐藏descriptionLabel后,应在更改约束后调用layoutIfNeeded方法。在更改约束时,您应该更改其优先级。 bottomLabeltopLabel约束的优先级应该很高,bottomLabeldescriptionLabel约束的优先级应该很低。这比删除和添加标签更好。

相关问题