我使用SDWebImage加載和顯示TableView中的異步圖像。但有時當我快速上下滾動時,它會混合所有圖像並將其顯示在其他行中。這是我的cellForRowAtIndexPath:表視圖混合圖像
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell.
MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
if (item) {
// Parse out Image URL for cell
if (cell.imageView.image == nil) {
NSError *error = NULL;
NSRegularExpression *regexImage = [NSRegularExpression regularExpressionWithPattern:@"(<img\\s[\\s\\S]*?src\\s*?=\\s*?['\"](.*?)['\"][\\s\\S]*?>)+?"
options:NSRegularExpressionCaseInsensitive
error:&error];
[regexImage enumerateMatchesInString:item.content
options:0
range:NSMakeRange(0, [item.content length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSString *src = [item.content substringWithRange:[result rangeAtIndex:2]];
NSLog(@"img src: %@", src);
[cell.imageView setImageWithURL:[NSURL URLWithString:src]];
}];}
if (cell.imageView.image == nil) {
cell.imageView.image = [UIImage imageNamed:@"video.png"];
}
return cell;
}
我不知道什麼是錯的,但我認爲它是因爲我在解析細胞的圖像,所以它一次又一次啓動它是不夠快。你能告訴我如何解決這個問題嗎?
它的作品,非常感謝你! –
很高興能幫到:) –
@ Supporter_04,這可能有效,但它在我看來好像爲每個數據項創建了一個新的單元格,因此您不會獲得任何單元格的重用。這打破了UITableView的內存效率。查理的答案可能很難實現(你需要正確理解SDWebImage的細節),但是它遵守細胞重用的原則。 – foundry