I'm new to iOS development and this is my first question on stackoverflow even though I come here a lot. Thanks for such a great resource!
I'm taking the Stanford CS193P course and having trouble with "assignment 5 extra credit 1".
I have a UITableView that displays title, subtitle, and a thumbnail. I queue up the thumbnail fetch but need to verify the table cell hasn't been recycled when the thumbnail image comes back.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Photo";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *imageDescription = [self.photoList objectAtIndex:indexPath.row];
NSString *expectedPhotoID = [imageDescription objectForKey:FLICKR_PHOTO_ID];
// Configure the cell...
cell.imageView.image = [UIImage imageNamed:@"placeholder.png"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *imageThumb = [self imageForCell:imageDescription];
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]]; // simulate 2 sec latency
dispatch_async(dispatch_get_main_queue(), ^{
NSString *photoID = [imageDescription objectForKey:FLICKR_PHOTO_ID];
if ([expectedPhotoID isEqualToString:photoID]) {
cell.imageView.image = imageThumb;
} else {
NSLog(@"cellForRowAtIndexPath: Got image for recycled cell");
}
});
});
return cell;
}
In this code, photoID
always matches expectedPhotoID
. I'm assuming it's because the imageDescription
pointer is used for both queues at the time they are created. I've tried using [self.photoList objectAtIndex:indexPath.row]
directly (in place of imageDescription
) but that didn't work either. It appears that too is resolved at the time the queues are created.
I'm missing some fundamental understanding here and appreciate your help.