UICollectionView重复单元格

Ram*_*ain 7 iphone ios uicollectionview uicollectionviewcell

我有一个UICollectionView显示从Internet下载的图像网格的地方.我正在使用SDWebImage加载图像.我面临的问题是,当我滚动浏览时UICollectionView,有时单元格会重复,显示相同的图像.但是当单元格滚出视图然后返回时,它具有正确的图像集.

-(UICollectionViewCell*) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    NSString *CellIdentifier = @"Gallery_Cell";

    GalleryCell *cell;

    if (cell==nil) {
        cell= (GalleryCell *)[self.flowCollection dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

        ImageItem *dets = [self.imageList objectAtIndex:indexPath.row];

        NSURL *mainImageURL = [NSURL URLWithString:dets.smallImageURL];

        cell.image.contentMode = UIViewContentModeScaleAspectFill;
        cell.image.clipsToBounds = YES;                

        [cell.image setImageWithURL:mainImageURL placeholderImage:nil];

    }

    return cell;

}
Run Code Online (Sandbox Code Playgroud)

这有发生在其他人身上吗?非常感谢任何指针.

edu*_*udi 7

GalleryCell.m你需要添加prepareForReuse方法,有废掉的_image变量(假设你有一个image在细胞@property):

- (void)prepareForReuse {
    [super prepareForReuse];
    [self setHighlighted:NO];

    _image = nil;
}
Run Code Online (Sandbox Code Playgroud)


小智 5

"UICollectionView类参考"中说明了以下内容:"如果您为指定的标识符注册了类,并且必须创建新的单元格,则此方法通过调用其initWithFrame:方法来初始化单元格.对于基于nib的单元格,此方法从提供的nib文件加载单元格对象.如果现有单元格可用于重用,则此方法将调用单元格的prepareForReuse方法."

你有GalleryCell单元类的prepareForReuse方法吗?

你的单元类应该是UICollectionReusableView类的子类.检查一下.


Ram*_*ain 1

我最终使用 UICollectionView 的 \xe2\x80\x9cdidEndDisplayingCell\xe2\x80\x9d 方法并结束当前图像下载并将图像设置为零。这工作完美,不再有 \xe2\x80\x9cshuffling\xe2\x80\x9d 或图像重复!:)

\n