开发者

TableView reload issue

开发者 https://www.devze.com 2023-04-03 10:54 出处:网络
Each cell of my TableView contains UIImage, I made an action to change all images in 开发者_如何学JAVAUIImage in each cell, then reload the table.

Each cell of my TableView contains UIImage, I made an action to change all images in 开发者_如何学JAVAUIImage in each cell, then reload the table.

The images didn't change until I scroll the table.

I know the TableView is loading cell during scrolling.

But is there a way to fix my issue?

Thanks


Make sure you are calling reloadData or beingUpdates/endUpdates from the MainThread. You can check this via:

if (![NSThread mainThread]) {
    return;
}

Here is an example. The ViewController is a delegate of an ImageDownloader, and when the image is done downloading it called -acceptImage, which then performs a selector on the main thread to do the table updates. Also, see the UITableView Reference

- (void)setDownloadedImage:(NSMutableDictionary *)d {
    UIImageView *imgV = (UIImageView *)[d objectForKey:@"imageView"];
    NSIndexPath *indexPath = [d objectForKey:@"userInfo"];
    [thumbs addObject:[ImageWorks imageWithImage:[UIImage imageWithData:[d objectForKey:@"image"]] scaledToSize:imgV.frame.size]];
    [table beginUpdates];
    [table insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
    [table endUpdates];
    loadingView.hidden = YES;
}

- (void)acceptImage:(NSData *)image ForUserInfo:(id)userInfo ForUrl:(NSString *)url {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSMutableDictionary *d = [[NSMutableDictionary alloc] init];
    iPhoneMyTableCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"iPhoneMyTableCell" owner:self options:nil] lastObject];
    [d setObject:userInfo forKey:@"userInfo"];
    [d setObject:(image ? image : UIImagePNGRepresentation([UIImage imageNamed:@"ImageMissing.png"])) forKey:@"image"];
    [d setObject:cell.myImageView forKey:@"imageView"];
    [self performSelectorOnMainThread:@selector(setDownloadedImage:) withObject:d waitUntilDone:YES];
    [d release];
    [pool drain];
}


Did you call [tableView reloadData];?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号