0

when I add label in UItablevewcell if it is as nil in contentview of cell.If it is not nil, I am taking that label through tag without allocating.it is the right procedure to reuse the cell. but when I dont want that label in second row , i have to hide it.How can I remove the label in second row only without hiding.I need it in first row.

2 Answers 2

0

For example you can use different cell identifiers when dequeue and create them. @"Cell With Label" and @"Cell Without Label" for instance.

Or you can tag this label by label.tag = MY_INT_TAG and search it by UILabel *label = [cell viewWithTag:MY_INT_TAG] to remove it from super view in the second row. It works when you don't want to subclass UITableViewCell.

if (indexPath.row == 0) {
  UILabel *label = [[UILabel alloc] init];
  label.tag = TAG;
  [cell.contentView addSubview:label];
} else if (indexPath.row == 1) {
  UILabel *label = [cell.contentView viewWithTag:TAG];
  [label removeFromSuperView];
}
0

When you are reusing the cells which have no common elements, the best practice is to clear the cell subviews (all added elements) before reusing it.

This way you can add the elements each time depending on your needs...

You can do this:

    for(UIView *view in cell.contentView.subviews){
        [view removeFromSuperview];
    }

or if you want to be more fancy:

    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

Of course if you want to clear only 1 particular element in a particular row, then you must assign a unique tag to the element when you add it to the cell's contentview, then remove it by accessing it through it's tag value:

Add it to the cell:

UIImageView *rightArrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png"]];
rightArrow.tag = 111;
rightArrow.frame = CGRectMake(290, 16, 4, 8);
[cell.contentView addSubview:rightArrow];

Remove it from the view for row 2:

if (indexpath.row == 2) {
    UIImageView *rightArrow = (UIImageView *)[cell.contentView viewWithTag:111];
    if (rightArrow)
      [rightArrow removeFromSuperView];

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.