Last active
September 1, 2018 17:24
-
-
Save keighl/6319942 to your computer and use it in GitHub Desktop.
SDWebImage in a UITableViewCell - cancel any download operation before reusing the cell. Otherwise, you might see previous images loading in before the correct one is finished downloading.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (UITableViewCell *)tableView:(UITableView *)tableView | |
cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
static NSString *identifier = @"MoveViewCell"; | |
CBMoveView *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; | |
if (!cell) | |
cell = [[CBMoveView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; | |
cell.move = (CBMove *)[self.moves objectAtIndex:indexPath.row]; | |
[cell configure]; | |
return cell; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// AFTER | |
@interface CBMoveCell | |
@property (weak) id <SDWebImageOperation> imageOperation; | |
@end | |
//////////// | |
- (void)prepareForReuse | |
{ | |
[super prepareForReuse]; | |
if (self.imageOperation) | |
[self.imageOperation cancel]; | |
self.imageOperation = nil; | |
} | |
- (void)configure | |
{ | |
// some stuff | |
if (self.move.image) | |
{ | |
SDWebImageManager *manager = [SDWebImageManager sharedManager]; | |
self.imageOperation = [manager downloadWithURL:[NSURL URLWithString:self.move.image] | |
options:SDWebImageRetryFailed | |
progress:nil | |
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { | |
if (image) | |
self.moveImageView.image = image; | |
}]; | |
} | |
// some other stuff | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// BEFORE | |
- (void)configure | |
{ | |
// some stuff | |
if (self.move.image) | |
{ | |
// Download or retrieve the image from the cache via SDWebImage | |
[self.moveImageView setImageWithURL:[NSURL URLWithString:self.move.image] | |
placeholderImage:[UIImage imageNamed:@"placeholderImage"] | |
options:SDWebImageRetryFailed]; | |
} | |
// some other stuff | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment