ios - 一种清空 UITableViewCell 上的 UIImageView 的方法

标签 ios objective-c uitableview uiimageview

我在 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/

相关文章:

objective-c - UIViewController 在 prepareForSegue 上抛出无法识别的选择器异常

ios - 使用 S3 下载功能异步快速更新 tableview

ios - 通过 UITableView 保存到 SQLITE3 时的奇怪行为

ios - 键盘阻塞 UITableViewCell 的 UITextField?

ios - 重启应用后如何恢复下载任务(NSURLSessionDownloadTask)

ios - 模拟位置,包括 Xcode 4.2/iOS5 中的移动

ios - 从目录获取文件到NSData

ios - 如何一次从 iTunes 购买所有从 iTunes 搜索 API 返回的歌曲?

ios - 适用于 iPad 的可滚动 GridView

ios - 为什么 sizeThatFits 不能与 UILabel 一起使用?