从UITableViewCell中删除UILabel并再次添加它们

时间:2016-04-20 09:30:22

标签: ios objective-c uitableview

我正在删除UITableViewCell中的标签,因为我不需要在该特定单元格中使用它们。问题是当细胞被重复使用时我需要它们但是它们之前被移除了。

if (post.blockContent == TRUE) {
        [cell.titleLabel removeFromSuperview];
        [cell.contentLabel removeFromSuperview];
}

如何将它们再次添加到UITableViewCell

我删除了它们,因为我有限制将所有内容与动态单元格高度相关联,我不能简单地隐藏它们,因为这只会在单元格的中间形成一个空白区域。

2 个答案:

答案 0 :(得分:2)

喜欢

  // set visibile for all cell
 [cell.contentView addSubview:cell.titleLabel];
 [cell.contentView addSubview:cell.contentLabel];
 // when contindition statisfy it will be hide
if (post.blockContent == TRUE) {
    [cell.titleLabel removeFromSuperview];
    [cell.contentLabel removeFromSuperview];
 }

<强>选择-2

 cell.titleLabel.hidden = NO;
 cell.contentLabel.hidden = NO;

  if (post.blockContent == TRUE) {
    cell.titleLabel.hidden = YES;
   cell.contentLabel.hidden = YES;
  }

答案 1 :(得分:0)

tableview重用了单元格。 因为这个原因你不能使用removeFromSuperview,因为使用相同实例的所有单元格都会删除标签。

解决方案是使用的约束。 你需要将标签包装起来进行查看,其他对象将具有跳过视图的约束并减少单元格的常量。

在heightForRow中,你需要在没有视图的情况下计算高度。

在运行时更改优先级,这是解决方案的主要思想。