我已經創建了一個UITableView,它可以從URL請求中填充每個單元格。我使用'dispatch_queue'來防止UItableView凍結。由於某種原因,當我滾動UITableView時,圖像閃爍並消失,並填充錯誤的單元格,直到修復它們自己。這是我的代碼。我正在使用restkit提供飼料UITableView中的圖像在滾動時不斷重新加載和錯誤的圖像閃爍
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;
當您滾動您的每一次細胞會重新下載圖像。我更喜歡使用(https://github.com/rs/SDWebImage)。這是很好的庫,下載圖像異步,它也保持緩存。因爲根據你的代碼。如果你的單元格索引爲0to5下載圖像。當你再次回到索引0to5時,你的圖像將再次下載。 – 2014-11-06 09:36:55