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

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

问题描述

我有一个带有自定义UICollectionViewCell类的UICollectionView,它在其contentView中添加了另一个UIView(thumbnailView)。另一个视图有一个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?

推荐答案

如果我理解你的问题,你会得到旧图像仍然出现在重用的单元格中,因此除了停止先前的图像加载之外,您可能还想从imageView中删除旧图像。 SDImageView可能有清除图像的方法,否则你只需用占位符图像手动替换它。

If I understand your problem correctly, you are getting old images still appearing in reused cells so in addition to stopping the previous image load you probably want to remove the old image from the imageView. SDImageView might have a method to clear the image or else you could just manually replace it with a placeholder image.

- (void)prepareForReuse
{
    [super prepareForReuse];
    [self.thumbnailView.imageView cancelCurrentImageLoad];
    [self.thumbnailView.imageView setImage:<<LOCAL PLACEHOLDER IMAGE>> forState:UIControlStateNormal];
}

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

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