Search code examples
iphonecocoa-touchuitableviewuikitcell

remove image from tableViewCell created programmatically


I would like to move an imageView contained on a cell and it works but creating a new image at new position keeping old image (then two are shown). How could I remove old one?? used code:

UIImage *cellImage = [UIImage imageNamed:(@"%@", showIconName)];
UIImageView *imageViewToPutInCell = [[UIImageView alloc] initWithImage:cellImage];

imageViewToPutInCell.frame = CGRectMake(iconPos, 7, cellImage.size.width, cellImage.size.height); 
[cell.contentView addSubview:imageViewToPutInCell];

every time that I reload tableView, it creates a new overlapped image.

[cell.contentView removeFromSuperview]; used before, removes it but then new image is not created.


Solution

  • There are a couple of ways to do this. Without subclassing, you could set the tag on your imageViewToPutInCell, then later on, use -[UIView viewWithTag:] to find and remove the view:

    int            imageViewTag = 1234;
    
    [[cell.contentView viewWithTag: imageViewTag] removeFromSuperview];
    imageViewToPutInCell.tag = imageViewTag;
    ...