繁体   English   中英

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

[英]Remove UILabels from UITableViewCell and add them again

我要从UITableViewCell删除标签,因为在该特定单元格中不需要它们。 问题是当单元被重用时,我需要它们,但之前已将它们删除了。

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

如何将它们再次添加到UITableViewCell

我删除它们是因为我在约束所有内容与动态单元格高度之间存在约束,而我不能简单地隐藏它们,因为这只会在单元格的中间留出空白。

喜欢

  // 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;
  }

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

解决方案是使用的约束。 您需要包装标签以查看,其他对象将具有跳过该视图并减小单元格常数的约束。

在heightForRow中,您需要计算没有视图的高度。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM