Reputation: 1158
I'm having a problem which is similar to others on SE, in that my UITableView controller loads the text label immediately, but only loads my thumbnail image when I scroll the view and move the item offscreen.
I tried adding [self.tableView reloadData] to the AFHTTPRequestOperation setCompletionBlockWithSuccess, which works with one drawback. It obviously runs too often.
Here is the method in which the problem occurs:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *fullPath;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath];
Child *child = _children[indexPath.row];
if([child.data.thumbnail length] == 0) {
fullPath = @"reddit.png";
} else {
// Get the thumbnail
NSURL *url = [NSURL URLWithString:child.data.thumbnail];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
fullPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[url lastPathComponent]];
[operation setOutputStream:[NSOutputStream outputStreamToFileAtPath:fullPath append:NO]];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
NSLog(@"bytesRead: %lu, totalBytesRead: %lld, totalBytesExpectedToRead: %lld", (unsigned long)bytesRead, totalBytesRead, totalBytesExpectedToRead);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// [self.tableView reloadData];
NSLog(@"RES: %@", [[[operation response] allHeaderFields] description]);
NSError *error;
if(error) {
NSLog(@"ERR: %@", [error description]);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"ERR1: %@", [error description]);
}];
[operation start];
}
cell.textLabel.text = child.data.title;
cell.imageView.image = [UIImage imageNamed:fullPath ];
return cell;
}
Upvotes: 0
Views: 44
Reputation: 375
Instead of reloading the entire table view every time an image is loaded, you could just set that image directly on the cell inside you completion block.
HOWEVER, if you do that you need to check that the cell is still visible and that it is still on the same index path it was on when you started loading the view, otherwise you might be setting the image on a cell that has been reused and is now in a different position in the table view.
Upvotes: 1