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];
}
}
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.
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;
}
}
}
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];
and also to set cell as:
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.