gpt4 book ai didi

ios - 如果可见,UICollectionViewCell 不更新图像

转载 作者:搜寻专家 更新时间:2023-11-01 05:56:10 25 4
gpt4 key购买 nike

我有一个 CollectionView,它在加载后显示缩略图。一开始,图片没有存储在设备上,所以它们是从服务器上获取的。在从服务器获取这些图像后的回调方法中,它们的单元格没有得到更新,我需要将它们滚动出屏幕,然后它们得到更新。我在另一个应用程序中有类似的机制,一切正常,但我找不到区别。下面是代码的一些部分,以便更好地理解我的工作。

这是我在检查是否已从服务器获取缩略图后设置图像的代码。如果是,直接设置,如果不是,使用Alamofire获取图片,加载完成后更新。

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.reuseIdentifier, for: indexPath) as! PictureCollectionViewCell

/* Set thumbnail to the cell */
if let thumbnail = pictures[indexPath.row].thumbnail {
cell.imageView.image = thumbnail
} else {
pictures[indexPath.row].retrievePicture(thumbnail: true, completion: { picture in
cell.imageView.image = picture
})
}

return cell
}

如前所述,在我将单元格滚动出可见屏幕区域之前,它不会更新。单元格或 collectionView 重新加载没有解决问题。有什么想法吗?

提前致谢。

最佳答案

因为该 block 在后台线程中执行。您必须在主线程中更新 UI。使用下面的代码

pictures[indexPath.row].retrievePicture(thumbnail: true, completion: { picture in
DispatchQueue.main.async {
cell.imageView.image = picture
}
})

关于ios - 如果可见,UICollectionViewCell 不更新图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43582861/

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