简体   繁体   English

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

[英]Remove UILabels from UITableViewCell and add them again

I'm removing labels from a UITableViewCell because i don't need them in that particular cell. 我要从UITableViewCell删除标签,因为在该特定单元格中不需要它们。 The problem is when the cell is reused i need them but they were removed before. 问题是当单元被重用时,我需要它们,但之前已将它们删除了。

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

How do i add them again to the UITableViewCell ? 如何将它们再次添加到UITableViewCell

I remove them because i have constraints linking everything with a dynamic cell height and i can't simply hide them because that will just make a empty space in the middle of the cell. 我删除它们是因为我在约束所有内容与动态单元格高度之间存在约束,而我不能简单地隐藏它们,因为这只会在单元格的中间留出空白。

do like 喜欢

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

choice-2 选择2

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

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

the tableview reused the cell. tableview重用了单元格。 because this reason you cant used removeFromSuperview because all the cells that used the same instance will remove the labels. 因为这个原因,您不能使用removeFromSuperview,因为使用同一实例的所有单元都将删除标签。

the solution is the used constraint. 解决方案是使用的约束。 you need to wrap the labels to view, and the other objects will have constraint that skip over the view and reduce the constant of the cell. 您需要包装标签以查看,其他对象将具有跳过该视图并减小单元格常数的约束。

in the heightForRow you need to calc the height without the view . 在heightForRow中,您需要计算没有视图的高度。

changed priority at runtime , its the main idea for the solution. 在运行时更改优先级,这是解决方案的主要思想。

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

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