下载延迟图像时更新UITableViewCell

时间:2009-12-18 00:09:16

标签: iphone uitableview

我在异步下载单元格图像后更新我的UITableViewCells时遇到了一些问题。我正在使用自定义UITableViewCells:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"productCell";
    MainTableViewCell *cell = (MainTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[MainCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"productCell"] autorelease];
    }
}

我有一个类是UITableViewCell,然后是一个完成所有绘图的类。这是来自Apple的示例代码,名为“AdvancedTableViewCells”,我正在进行复合。

我的单元格中有图像,我使用Apple的示例代码“LazyTableImages”异步下载。这是应该更新单元格的委托:

- (void)coverImageDidLoad:(NSIndexPath *)indexPath {
    CoverImageAsyncLoader *coverImageAsyncLoader = [imageDownloadsInProgress objectForKey:indexPath];
    if (coverImageAsyncLoader != nil) {
        MainTableViewCell *cell = (MainTableViewCell *)[self.tableView cellForRowAtIndexPath:coverImageAsyncLoader.indexPathInTableView];

        // Display the newly loaded image
  if (coverImageAsyncLoader.products.coverImage != nil) {
   cell.productCover = coverImageAsyncLoader.products.coverImage;
  } else {
   cell.productCover = blankCoverImage;
  }
    }
}

但没有任何反应。一位朋友告诉我,我无法从后台线程更新UI,但由于我是通过委托进行的,因此我不确定它为什么不更新。我试过了:

 [cell setNeedsDisplay];
 [cell.contentView setNeedsDisplay];

并将单元格设置为:

 cell = [[[MainCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"productCell"] autorelease];

然后更新单元格的显示。

但没有任何作用:(我可以发布更多代码,但帖子已经有点长了。

1 个答案:

答案 0 :(得分:3)

如果您知道相关单元格的索引路径的行和部分(例如myCellIndexPath.sectionmyCellIndexPath.row),请查看使用表格视图{{}重新加载UITableView单元格1}}方法。

例如:

-reloadRowsAtIndexPaths:withRowAnimation:

由于这会更新UI,因此您需要在主线程上运行的方法中使用此功能。

还有其他行动画,具体取决于品味([tableView beginUpdates]; NSUInteger _path[2] = {myCellIndexPath.section, myCellIndexPath.row}; NSIndexPath *_indexPath = [[NSIndexPath alloc] initWithIndexes:_path length:2]; NSArray *_indexPaths = [[NSArray alloc] initWithObjects:_indexPath, nil]; [_indexPath release]; [tableView reloadRowsAtIndexPaths:_indexPaths withRowAnimation:UITableViewRowAnimationRight]; [_indexPaths release]; [tableView endUpdates]; UITableViewRowAnimationFade等)。在UITableViewRowAnimationNone结构上搜索您的帮助。

相关问题