1

In my cellForRowAtIndexPath method, I have this code:

if ([self.cellImageCache objectForKey:indexPath]) {
        UIImage *image = [self.cellImageCache objectForKey:indexPath];
        [cell.entryView setImage:image];

        return cell;
    }

Entry *entry = [self.appDelegate.fetchedResultsController objectAtIndexPath:indexPath];

UIImage *image = [JTimelineCellContent imageForEntry:entry];

[cell.entryView setImage:image];

[self.cellImageCache setObject:image forKey:indexPath];

When I scroll to the bottom of the tableview, each cell displays fine. Upon scrolling back up, cells display fine too. But when I begin scrolling down after scrolling up, cells begin to appear repeated.

Every NSLog search I do for index paths returns the correct value, even on repeated cells.

4

1 回答 1

1

NsIndexPath 实际上是一个数组而不是字符串。所以你的设计是有缺陷的。

您可以使用以下方法从 indexPath 生成唯一字符串:

NSString *uniqueKey = [NSString stringWithFormat:@"key%@%@",indexPath.section,indexPath.row];

所以你可以拥有:

NSString *uniqueKey = [NSString stringWithFormat:@"key%@%@",indexPath.section,indexPath.row];
  if ([self.cellImageCache objectForKey:uniqueKey]) {
            UIImage *image = [self.cellImageCache objectForKey:uniqueKey];
            [cell.entryView setImage:image];

            return cell;
        }

    Entry *entry = [self.appDelegate.fetchedResultsController objectAtIndexPath:indexPath];

    UIImage *image = [JTimelineCellContent imageForEntry:entry];

    [cell.entryView setImage:image];

    [self.cellImageCache setObject:image forKey:uniqueKey];
于 2013-01-10T23:15:45.090 回答