I am trying to load locally stored images in a background thread and set them as UITableViewCell images. I keep getting an exception and don’t really know how to approach it.
Here is my code:
- (void)loadImageInBackground:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *path = [[tableImages objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
UIImage *img = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:path ofType:@"png"]];
[self performSelectorOnMainThread:@selector(assignImageToImageView:) withObject:img waitUntilDone:YES];
[img release];
[pool release];
}
- (void) assignImageToImageView:(UIImage *)img
{
UITableViewCell *cell = (UITableViewCell *)[self.tableView viewWithTag:0];
((UIImageView *)cell.imageView).image = img;
}
Anyone have any ideas? Thanks to all!
As is mention in your error you are trying to set
imageto object of classUITableView. It is impossible.In your code it is error here:
By default all views has default view
tagset to0. When your are calling that method to yourtableViewit returns you firstUIViewwithtag == 0. I think you didn’t change default tag of yourUITableView *tableViewso that method returns youtableViewand notUITableViewCell.In this case, you need to choose right way to access appropriate
UITableViewCell *cell;.