I have this method and I was advised to do the download of images on the background thread. Can anyone help me with this?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)index开发者_JS百科Path
{
NSURL *myURL=[NSURL URLWithString:[self.picturesArray objectAtIndex:indexPath.row]];
NSData *myData1 = [[NSData alloc] initWithContentsOfURL:myURL];
UIImage *myImage = [[UIImage alloc] initWithData:myData1];
cell.imageView.image = myImage;
return cell;
}
Setting aside the fact that the code shown couldn't possibly work by itself... what you want to do is pretty easy. Your cell could be a custom UITableCell in which you define a method that does your download in the background. In cellForRowAtIndexPath:, you'd call that method, e.g. [cell loadImageInBackground:myURL]
. The clever part is that the cell, of course, knows what is in it, i.e. the UIImageView you want to setup. So the background load, when it completes, can just set the image, and your table remains responsive.
You could use a UIImageView that downloads asynchronously. Check this one out: http://iphone-dev-tips.alterplay.com/2009/10/asynchronous-uiimage.html
You should create a UITableViewCell subclass and add an AsynchronousImageView to it and then do [cell.asyncImageView loadImageFromURLString:[self.picturesArray objectAtIndex:indexPath.row]].
精彩评论