我有一些有趣的事情发生了。 tableview通过其API加载flickr照片。加载视图后,它会创建一个名称& photoURL数组。然后在cFRAIP tableview方法中,它使用它们来设置单元格值。
我决定玩GCD并最终得到这个:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell Identifier"] autorelease];
cell.textLabel.text = [photoNames objectAtIndex:indexPath.row];
dispatch_async(kfetchQueue, ^{
NSData *imageData = [NSData dataWithContentsOfURL:[photoURLs objectAtIndex:indexPath.row]];
cell.imageView.image = [UIImage imageWithData:imageData];
});
return cell;}
但我得到的是一个桌面视图,每个单元格中没有图片的照片名称。当我点击单元格时,图片才会加载。为什么会这样?
答案 0 :(得分:2)
Manipulations to your application’s user interface must occur on the main thread.
dispatch_async(kfetchQueue, ^{
NSData *imageData = [NSData dataWithContentsOfURL:[photoURLs objectAtIndex:indexPath.row]];
dispatch_async(dispatch_get_main_queue(), ^{
cell.imageView.image = [UIImage imageWithData:imageData];
});
});
答案 1 :(得分:0)
哦,我明白了,你有一个小错字:
dispatch_async(kfetchQueue, ^{
//NSData *imageData = [NSData dataWithContentsOfURL:[photoURLs objectAtIndex:indexPath.row]];
dispatch_async(dispatch_get_main_queue(), ^{
//cell.imageView.image = [UIImage imageWithData:imageData];
}); //<-------right here :) thx!
});
然而,结果仍然相同。它仅在我触摸时显示图像。我认为这是因为当它排队等待获取NSData时。我必须在init方法中而不是在cFRAIP方法中加载照片。