我在 UITableViewCell
上有一个 UIImageView
当我没有要查看的图像时,我希望它消失。
我已经实现了这段代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ((item.content_image != nil) && (item.content_image != (id)[NSNull null]))
{
[cell.imageContentImage setImageWithURL:[NSURL item.content_image]] ;
}
else
{
cell.imageContentImage = nil;
}
}
我收到一位用户的报告,指出单元格中的图像错误(它似乎复制了其他单元格中的图像)。
问题是它不会发生在模拟器或我可以调试它的设备上,所以我的问题变成了理论问题:
将 nil
分配给 UIImageView
是正确的方法吗?有没有“更正确”的方式?
最佳答案
这里有几个问题。首先,当我认为您正在寻找的更像是这样的东西时,您正在对 NSNull
类型的对象进行指针比较:
if(item.content_image && ![item.content_image isEqual:[NSNull null]]) {}
其次,您可能会看到重复图像的原因是因为您不是每次都分配新的单元格,而是出队
一个可重用的单元格,其中可能有旧内容遗留在其中。最简单的方法是立即简单地 nil
ImageView 的图像属性,然后仅在图像存在时分配图像。
cell.imageContentImage.image = nil;
if(item.content_image && ![item.content_image isEqual:[NSNull null]])
{
[cell.imageContentImage setImageWithURL:[NSURL urlWithString:item.content_image]];
}
关于ios - 一种清空 UITableViewCell 上的 UIImageView 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25810900/