What is the best way to retry a failed image download in cellForItemAtIndexPath? I have a collection view that can show about 20 80x80 thumbnail images. I'm using SDWebImage to download and cache images in the cellForItemAtIndexPath method, but I've found that every so often an image fails to download. I know I can use a completion block and test if the image is nil, but it doesn't seem to be reloading the image correctly (the image still never shows up). Is this how I should be handling reloading the cell? Here's an example of my code.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {   
    NSURL *thumbnailURL = [NSURL URLWithString:url];

    INCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    [cell.imageView setImageWithURL:thumbnailURL completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
        if (!image) {
            [self.collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
        }
    }];

    return cell;
}
有帮助吗?

解决方案

Got an answer on github

You need to use one of the methods with an "options" parameter and specify SDWebImageRetryFailed like so...

[cell.imageView setImageWithURL:thumbnailURL placeholderImage:[UIImage imageNamed:@"placeholder.png"] options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {...}];

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top