set UITableView image
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:indexPat开发者_C百科h.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 image
to object of class UITableView
. It is impossible.
In your code it is error here:
UITableViewCell *cell = (UITableViewCell *)[self.tableView viewWithTag:0];
By default all views has default view tag
set to 0
. When your are calling that method to your tableView
it returns you first UIView
with tag == 0
. I think you didn't change default tag of your UITableView *tableView
so that method returns you tableView
and not UITableViewCell
.
In this case, you need to choose right way to access appropriate UITableViewCell *cell;
.
精彩评论