异步下载单元格图像后,我在更新 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];
然后更新单元格的显示。
但没有任何效果:(我可以发布更多代码,但帖子已经有点长了。