I have looked around on SO and the web and have not found a specific answer.
Quite simply, I have a table loading images info from Flickr. I want to display a thumbnail of the image on the left side of each cell.
In order to do this without blocking the main (UI) thread, I am using blocks:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"top50places";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//getting the selected row image
NSDictionary* currentImageDictionary=[self.topfifty objectAtIndex:indexPath.row];//topFifty is an array of image dictionaries
//creating the download queue
dispatch_queue_t downloadQueue=dispatch_queue_create("thumbnailImage", NULL);
dispatch_async(downloadQueue, ^{
UIImage *downloadedThumbImage=[self getImage:currentImageDictionary] ;
//Need to go back to the main thread since this is UI related
dispatch_async(dispatch_get_main_queue(), ^{
cell.imageView.image = downloadedThumbImage ;
});
});
dispatch_release(downloadQueue);
return cell;
}
Now this is not going to work. Because returning the cell will likely happens before executing the block. But at the same time, I cannot return the cell within the main queue block because the block does not accept a return argument.
I want to avoid creating a UITableViewCell subclass.
Any simple answer using blocks?
Thanks KMB
The problem with doing
cell.imageView.image = downloadedThumbImage;
is that this cell may now be reused and used for another row.Instead you need to update the current cell in the specific indexPath (the indexPath will be the same)
Or sometimes what I do is to update the model and then reload the cell at the specific indexPath: