I'm trying to put placeholder images in my table view cell. Is there an event to trigger a redraw of the cell contents once the image data is uploaded? I'm using the following to create the image:
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:photoURLString]]]
Side n开发者_C百科ote: I'm using ABTableViewCell
to manually draw the contents of the cell with the drawInRect
method.
Thanks.
That call is synchronous, so when it is complete your image is ready.
What you probably want to look into is loading image asynchronously and updating the image as they are loaded.
Something like:
NSURL *url = [NSURL URLWithString:imageURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
set a property to contain the NSData
and keep track of the NSURLConnection so you can properly release it.
self.activeDownload = [NSMutableData data];
self.imageConnection = conn;
then provide the proper delegate methods for handling the connection callbacks:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.activeDownload appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// Clear the activeDownload property to allow later attempts
self.activeDownload = nil;
// Release the connection now that it's finished
self.imageConnection = nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// Set appIcon and clear temporary data/image
UIImage *image = [[UIImage alloc] initWithData:self.activeDownload];
//MAKE Your callbacks and update the image here.
}
Your callback is going to want to obtain that image, and set it in such a way that a reload of the table view will be able to retrieve it properly. I.e. set the image in a data object with the rest of the data the table view cell uses.
Additionally, you can then do a single refresh of just the cell that needs it via:
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
This is from Apple's own LazyImageTables example.
You can do this yourself, however I'd suggest using EGOImageLoading, it's designed for this exact use. In your subclass of UITableViewCell, instead of using a UIImageView, use EGOImageView.
This blog post shows how to use it
and the code is on github
(if you download the code off github, using the download button on the site to get a zip or tar.gz file then you'll need to download EGOCache as well (linked to on the github page))
精彩评论