简体   繁体   中英

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. 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 ?

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

 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. because this reason you cant used removeFromSuperview because all the cells that used the same instance will remove the labels.

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 .

changed priority at runtime , its the main idea for the solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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