UITableView 单元格显示不正确的图像,即使将图像设置为 nil 作为 tableView.dequeueReusableCell 中的第一步 [英] UITableView cell is displaying an incorrect image, even while setting the image to nil as the first step in tableView.dequeueReusableCell

查看:19
本文介绍了UITableView 单元格显示不正确的图像,即使将图像设置为 nil 作为 tableView.dequeueReusableCell 中的第一步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一些非常基本的事情,但在其他类似问题中提出的修复似乎不起作用.我有一个图像缓存和一个 tableView.如果存在,我想显示缓存中的图像,否则应该什么都没有.出于某种原因,即使我将图像视图设置为 nil,tableView 仍然显示带有错误图像的重用单元格.下面是我的代码:

I'm trying to do something very basic, but the fix proposed in other similar questions does not seem to be working. I have an image cache, and a tableView. I want to display the image from the cache if it exists, otherwise there should be nothing. For some reason the tableView is still displaying a reused cell with the wrong image, even when I set the image view to nil. Below is my code:

let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell", for: indexPath) as! SearchResultsTableViewCell

    cell.profilePhoto?.image = nil
    cell.profilePhoto?.backgroundColor = UIColor.gray
    if let userID = myObject.posterId, let profileImage = self.imageCache.object(forKey: userID as AnyObject) {
        cell.profilePhoto?.image = profileImage
    } else {
        if let userId = myObject.posterId {
            downloadImage.beginImageDownload() {
                (imageOptional) in
                if let image = imageOptional {
                    cell.profilePhoto?.image = image
                    self.imageCache.setObject(image, forKey: userId as AnyObject)
                }
            }
        }
    }

我做错了什么?我一生都无法弄清楚为什么图像没有被设置为 nil,即使我是第一步这样做的!

What am I doing wrong? I can't for the life of me figure out why the image is not being set to nil, even though I do that as the first step!

推荐答案

问题是 downloadImage.beginImageDownload 闭包持有对 uitableview 单元格的引用.

The problem is downloadImage.beginImageDownload closures are holding references to the uitableview cells.

当您完成图片下载时,您设置了 cell.profilePhoto?.image 属性,即使 tableView 回收可重复使用的单元格以显示不同的行.

When you complete the image download, you set cell.profilePhoto?.image property, even if the tableView recycles reusable cells to display a different row.

将您单元格的 tag 分配给 indexPath.row 并测试单元格是否仍然与分配下载的图像相关:

Assign your cell's tag to the indexPath.row and test if cell is still relevant for assigning downloaded image:

/* right after cell dequeue */
cell.tag = indexPath.row

然后

/* download finished here */
if cell.tag == indexPath.row {
    /* yeah, I want to rock this cell with my downloaded image! */
    cell.profilePhoto?.image = downloadedImage
}

请注意:这仅适用于包含一个部分的 tableview.

附言您可以将单元格的 cleanup 放在 SearchResultsTableViewCell 内的 prepareForReuse 方法中,以便稍微整理一下.

P.S. You can place the clean up of your cells in prepareForReuse method inside the SearchResultsTableViewCell to tidy things a little.

这篇关于UITableView 单元格显示不正确的图像,即使将图像设置为 nil 作为 tableView.dequeueReusableCell 中的第一步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆