Created
January 31, 2011 08:13
-
-
Save evanlong/803766 to your computer and use it in GitHub Desktop.
infectmac.com blog post code sample about UITableView and GCD race condition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
static NSString *CellIdentifier = @"Cell"; | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; | |
if (cell == nil) { | |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; | |
} | |
NSDictionary* pictObj = [self.pictures objectAtIndex:indexPath.row]; | |
NSString* title = [pictObj objectForKey:@"title"]; | |
title = [title length] == 0 ? @"(no title)" : title; | |
cell.textLabel.text = title; | |
cell.detailTextLabel.text = [[pictObj objectForKey:@"description"] objectForKey:@"_content"]; | |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; | |
//identify which image the cell should be displaying right now | |
cell.tag = indexPath.row; | |
Photo* photo = [Photo photoWithFlickrData:pictObj inManagedObjectContext:context]; | |
if (photo.thumbnail) { | |
cell.imageView.image = [UIImage imageWithData:photo.thumbnail]; | |
} | |
else { | |
cell.imageView.image = nil; | |
[photo thumbnailWithBlock:^(UIImage* image) { | |
//check to make sure the image should be updated | |
if (cell.tag == indexPath.row) { | |
cell.imageView.image = image; | |
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; | |
} | |
}]; | |
} | |
return cell; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment