UICollectionView + SDWebImage + 单元重用 [英] UICollectionView + SDWebImage + Cell reuse

查看:17
本文介绍了UICollectionView + SDWebImage + 单元重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义 UICollectionViewCell 类的 UICollectionView,它将另一个 UIView (thumbnailView) 添加到它的 contentView.这个另一个视图有一个 UIImageView 属性(imageView),它的图像通过 SDWebImage 异步加载.这一切都可以正常工作,直到在原始单元格的图像仍在加载时重新使用单元格.如果您在 CollectionView 中滚动得足够快,图像开始在不应该出现的单元格中弹出(可能是因为单元格被重用并且图像下载随后完成),只要正确的图像被重用就被替换单元格完成加载.

I have a UICollectionView with a custom UICollectionViewCell class which adds another UIView (thumbnailView) to its contentView. This other view has a UIImageView property (imageView) whose image gets loaded with SDWebImage asynchronously. This all works fine until the cell is reused while the image of the original cell is still being loaded. If you scroll fast enough through the CollectionView images start to pop up in cells where they are not supposed to be (probably because the cell got reused and the image download finishes afterwards), just to get replaced as soon as the correct image for the reused cell finishes loading.

为了解决这个问题,我在 UICollectionViewCell 子类中添加了以下内容以停止原始单元格的图像下载:

To fix this, I added the following to my UICollectionViewCell subclass to stop the original cell's image download:

- (void)prepareForReuse
{
    [super prepareForReuse];
    [self.thumbnailView.imageView cancelCurrentImageLoad];
}

但令我惊讶的是,这似乎完全没有改变.是我做错了什么还是我只是看错了方向?

But to my surprise, this seems to change absolutely nothing. Am I doing something wrong or am I just looking in the wrong direction?

推荐答案

Swift 4 - 添加到单元格的类中:

Swift 4 - Add to your cell's class:

override func prepareForReuse() {
  super.prepareForReuse() 
  self.contentImageView.sd_cancelCurrentImageLoad()
  self.contentImageView.image = UIImage(named: "") // set to default/placeholder image
}

这篇关于UICollectionView + SDWebImage + 单元重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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