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

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

这是单元格的示意图
-----------------------------
    topLabel

    descriptionLabel

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

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

问题在于我假设是单元重用,当我向上/向下滚动并且已删除descriptionLabel的单元被重用时,不会重新添加。

我尝试过的替代品:
  • 隐藏descriptionLabel与删除bottomLabel相比,通过这种方式,它可以保持其框架,因此ojit_code不会向上移动。

  • 我是否需要重新初始化标签并将其添加到视图中?还是有更好的方法来处理此用例?

    最佳答案

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

    关于ios - 使用AutoLayout从UITableViewCell删除 subview ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32464370/

    10-14 20:57
    查看更多