gpt4 book ai didi

ios - UITableViewCell 图像加载错误的图像

转载 作者:行者123 更新时间:2023-11-29 00:31:03 25 4
gpt4 key购买 nike

现在我下载图像,将其记录在可变字典中,然后验证图像是否已下载,如果没有,则下载并存储它。我使用 indexPath 作为键。

这段代码有点工作,但从我所做的测试来看,如果我滚动太快,单元格图像将加载错误的图像,一秒钟后它将加载正确的图像(替换错误的图像)。

我总是在调用该方法后清除我的缩略图 (imageView),所以我不知道为什么我会遇到这个错误。

我虽然也许 if(self.imageCache.object(forKey: cacheKey) != nil) 语句是正确的,这就是为什么我会得到多个图像,但断点没有停止当我向下滚动时立即。

有什么想法吗?

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MovieCellController
cell.thumbnail.image = UIImage()

let cacheKey = indexPath.row

if(self.imageCache.object(forKey: cacheKey) != nil)
{
cell.thumbnail.image = self.imageCache.object(forKey: cacheKey) as? UIImage
}
else
{
DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
if let url = NSURL(string: self.moviesCollection[indexPath.row].imageLink) {
if let data = NSData(contentsOf: url as URL) {
let image: UIImage = UIImage(data: data as Data)!
self.imageCache.setObject(image, forKey: cacheKey as NSCopying)
DispatchQueue.main.async(execute: {
cell.thumbnail.image = image
})
}
}
}
}

cell.name.text = moviesCollection[indexPath.row].name

return cell

}

最佳答案

发生这种情况是因为单元格被重用,因此当快速滚动时,似乎分配了另一个单元格的图像,但实际上它是重用的前一个单元格的图像。

在单元格的 prepareForReuse 方法中,将您的 imageView 的图像设置为 nil。就像,imageView.image = nil

关于ios - UITableViewCell 图像加载错误的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41820427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com