Instead of grabbing the cell, you need to grab the index path and then return the cell using:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
Thus, if the cell is now turned off, you will get zero back, and the image will not be set to the wrong cell.
Another thing you need to add after dispatch_async() is cell.imageView.image=somePlaceholderImage .
eg:.
if (![[NSFileManager defaultManager] fileExistsAtPath:[path stringByAppendingPathComponent:@"image.png"]]) { dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); dispatch_async(queue, ^{ NSString *url=[pat stringByAppendingPathComponent:@"comments.txt"]; NSString *u=[NSString stringWithContentsOfFile:url encoding:NSUTF8StringEncoding error:nil]; NSURL *imageURL=[NSURL URLWithString:u]; NSData *image=[NSData dataWithContentsOfURL:imageURL]; [image writeToFile:[pat stringByAppendingPathComponent:@"image.png"] atomically:YES]; dispatch_sync(dispatch_get_main_queue(), ^{ UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.imageView.image=[UIImage imageWithContentsOfFile:[pat stringByAppendingPathComponent:@"image.png"]]; [cell setNeedsLayout]; NSLog(@"Download"); }); }); cell.imageView.image=[UIImage imageNamed:@"placeholder"]; } else { NSLog(@"cache"); cell.imageView.image=[UIImage imageWithContentsOfFile:[pat stringByAppendingPathComponent:@"image.png"]]; }
hypercrypt
source share