下载延迟图像时更新UITableViewCell [英] Update UITableViewCell when lazy image is done downloading
问题描述
我在异步下载单元格图像后更新我的UITableViewCells时遇到了一些问题。我正在使用自定义UITableViewCells:
I am having some issues with updating my UITableViewCells after asynchronously downloading the cell image. I am using custom UITableViewCells like so:
- (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,我正在复合。
I have a class that is the UITableViewCell and then a class that does all the drawing. It's the sample code from Apple called "AdvancedTableViewCells" and I am doing composite.
我的单元格中有图像,我使用Apple的示例代码LazyTableImages异步下载。这是应该更新单元格的委托:
I have images in my cells, which I download asynchronously using Apple's sample code called "LazyTableImages". Here's the delegate that should be updating the cell:
- (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,但由于我是通过委托进行的,因此我不确定它为什么不更新。我试过了:
But nothing happens. A friend tells me I cannot update the UI from a background thread, but since I am doing it through a delegate I am not sure why it isn't updating. I have tried:
[cell setNeedsDisplay];
[cell.contentView setNeedsDisplay];
并将单元格设置为:
cell = [[[MainCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"productCell"] autorelease];
然后更新单元格的显示。
and then update the cell's display.
但没有任何作用:(我可以发布更多代码,但帖子已经有点长了。
But nothing works :( I can post more code, but the post is already a bit long.
推荐答案
如果你知道索引路径的行和部分到您的单元格(例如 myCellIndexPath.section
和 myCellIndexPath.row
),看一看使用表视图的 -reloadRowsAtIndexPaths:withRowAnimation:
方法重新加载 UITableView
单元格。
If you know the index path's row and section to your cell in question (e.g. myCellIndexPath.section
and myCellIndexPath.row
), take a look at reloading a UITableView
cell with the table view's -reloadRowsAtIndexPaths:withRowAnimation:
method.
例如:
[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];
由于这会更新UI,因此您需要在主线程上运行的方法中使用此功能。
As this updates the UI, you will want this in a method that is run on the main thread.
还有其他行动画,具体取决于品味( UITableViewRowAnimationFade
, UITableViewRowAnimationNone
等)。在 UITableViewRowAnimation
struct上搜索您的帮助。
There are other row animations, depending on taste (UITableViewRowAnimationFade
, UITableViewRowAnimationNone
, etc.). Search your help on the UITableViewRowAnimation
struct.
这篇关于下载延迟图像时更新UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!