0

我有一些有趣的事情发生。表格视图通过其 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;}

但我得到的是一个表格视图,每个单元格中都有照片名称,没有图片。图片仅在我点击单元格时加载。为什么会这样?

4

2 回答 2

2

对应用程序用户界面的操作必须在主线程上进行。

dispatch_async(kfetchQueue, ^{
    NSData *imageData = [NSData dataWithContentsOfURL:[photoURLs objectAtIndex:indexPath.row]];
    dispatch_async(dispatch_get_main_queue(), ^{
        cell.imageView.image = [UIImage imageWithData:imageData];
    });
});
于 2013-01-05T08:19:45.593 回答
0

Oh I see, you had a small typo:

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!
});

However, the result is still the same. Its displaying the images only when i touch them. I think its because thats when its queued to fetch the NSData. I would have to load the photos in an init method instead of in the cFRAIP method.

于 2013-01-05T16:02:54.713 回答