问题描述
我想移动一个包含在单元格中的imageView,它可以工作,但是在新位置创建一个新图像,同时保留旧图像(然后显示两个)。我该如何删除旧的?使用的代码:
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];
每次重新加载tableView时,它都会创建一个新的重叠图像。
every time that I reload tableView, it creates a new overlapped image.
[cell.contentView removeFromSuperview];
[cell.contentView removeFromSuperview]; used before, removes it but then new image is not created.
推荐答案
有两种方法可以执行此操作。如果没有子类,则可以在imageViewToPutInCell上设置标签,然后再使用-[UIView viewWithTag:]查找并删除视图:
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;
...
这篇关于从以编程方式创建的tableViewCell中删除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!