I am experimenting in tableView:cellForRowAtIndexPath
to set an image by callingpost
[self downloadImage:urlString andSetIntoCellImageView:cell]
and in downloadImage
, it will call NSURLConnection:sendAsynchronousRequest
(iOS 5 and up only), and in the completion block, set the image by usingui
cell.imageView.image =[UIImage imageWithData:data];// data is downloaded data
and it works if in tableView:cellForRowAtIndexPath
, the imageView
is populated with a dummy placeholder image -- and I wonder how the new image is refreshed, is it by setNeedsDisplay
to do a repaint? But if I don't set the placeholder image, then the new image won't show at all. I wonder what mechanism can be used to make it show the image?this
If I use
[cell.imageView setNeedsDisplay]
or
[cell setNeedsDisplay];
in the completion block, it won't work, and if I use
[self.table reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
in the completion block by making downloadImage
accept the indexPath
, it will call tableView:cellForRowAtIndexPath
again, and cause an infinite loop. So it seems like I need to use some hash table to remember if the image is already in hash table: if not, call downloadImage
, and if in hash table, simply use it, so there will be no infinite loop.
But is there an easy way to cause the image to show up? Setting a placeholder image works but what if we don't -- by what mechanism does placeholder cause the refresh of image?
cell.imageView.image = [UIImage imageWithData:data]
works as long as there's been a placeholder image set in the cell within thetableView:cellForRowAtIndexPath:
method, but if you don't provide a placeholder, this same technique doesn't work? Would you mind posting how you're setting up the placeholder image? – Carl Veazey Aug 31 '12 at 5:34cell.imageView.image = [UIImage imageNamed:@"pic0.png"];
– 動靜能量 Aug 31 '12 at 5:44