【发布时间】:2014-11-06 09:07:11
【问题描述】:
我创建了一个 UITableView,它根据 URL 请求填充每个单元格。我使用“dispatch_queue”来防止 UItableView 冻结。出于某种原因,当我滚动浏览 UITableView 时,图像会闪烁并消失并填充错误的单元格一秒钟,直到自行修复。这是我的代码。我正在使用 restkit 提取提要
customCell.customCellTextLabel.text = [NSString stringWithFormat:@"%@",feedO.title];
NSString *string = [NSString stringWithFormat:@"%@",feedO.body];
NSURL *urlString;
NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray *matches = [linkDetector matchesInString:string options:0 range:NSMakeRange(0, [string length])];
for (NSTextCheckingResult *match in matches) {
if ([match resultType] == NSTextCheckingTypeLink) {
urlString = [match URL];
NSLog(@"found Body URL: %@ and title %@", urlString,feedO.title);
}
}
dispatch_queue_t imageQueue = dispatch_queue_create("imageDownloader",nil);
dispatch_async(imageQueue, ^{
NSData *data = [[NSData alloc] initWithContentsOfURL:urlString];
dispatch_async(dispatch_get_main_queue(), ^{
customCell.customCellImageView.image = [UIImage imageWithData: data];
});
});
return customCell;
【问题讨论】:
-
当您每次滚动时,您的单元格将再次下载图像。我更喜欢使用 (github.com/rs/SDWebImage)。这是一个很好的库,可以异步下载图像并且它还维护缓存。因为根据您的代码。如果您的单元格下载索引 0to5 的图像。当您再次返回索引 0to5 时,您的图像将再次下载。
标签: ios objective-c xcode uitableview ios8