为什么图像不会显示在单元格上?我想这与线程有关。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
GKTurnBasedMatch *match = [matches objectAtIndex:indexPath.row];
GKTurnBasedParticipant *part = [match.participants objectAtIndex:0];
[GKPlayer loadPlayersForIdentifiers:@[part.playerID] withCompletionHandler:^(NSArray *players, NSError *error) {
if(error == nil && players.count == 1) {
GKPlayer *player = [players lastObject];
[player loadPhotoForSize:GKPhotoSizeSmall withCompletionHandler:^(UIImage *photo, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
cell.imageView.image = [UIImage imageNamed:@"coins.png"]; //Doesnt work
});
}];
}
}];
//cell.imageView.image = [UIImage imageNamed:@"coins.png"]; //This Works
[cell.textLabel setText:part.playerID];
return cell;
}
下面的代码工作,谢谢
dispatch_async(dispatch_get_main_queue(), ^{
UITableViewCell *cellToUpdate = [self.tableView cellForRowAtIndexPath:indexPath];
if (cellToUpdate != nil && photo != nil) {
cellToUpdate.imageView.image = photo;
[cellToUpdate setNeedsLayout];
}
});