使用块加载表中的图像 – dispatch_async返回值

我已经环顾了这个网页,并没有find具体的答案。

很简单,我有一个表格从Flickr加载图像信息。 我想在每个单元格的左侧显示图像的缩略图。

为了做到这一点而不阻塞主(UI)线程,我正在使用块:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"top50places"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //getting the selected row image NSDictionary* currentImageDictionary=[self.topfifty objectAtIndex:indexPath.row];//topFifty is an array of image dictionaries //creating the download queue dispatch_queue_t downloadQueue=dispatch_queue_create("thumbnailImage", NULL); dispatch_async(downloadQueue, ^{ UIImage *downloadedThumbImage=[self getImage:currentImageDictionary] ; //Need to go back to the main thread since this is UI related dispatch_async(dispatch_get_main_queue(), ^{ cell.imageView.image = downloadedThumbImage ; }); }); dispatch_release(downloadQueue); return cell; } 

现在这不起作用。 因为返回单元格可能会发生在执行块之前。 但同时,我不能返回主队列块内的单元格,因为该块不接受返回参数。

我想避免创build一个UITableViewCell子类。

任何简单的答案使用块?

谢谢九巴

与做的问题cell.imageView.image = downloadedThumbImage; 是这个单元现在可以被重用并用于另一行。

相反,您需要更新特定indexPath中的当前单元格(indexPath将相同)

 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.imageView.image = image; 

或者有时我所做的是更新模型,然后重新加载特定索引path的单元格:

 myModel.image = downloadedThumbImage; [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"top50places"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //getting the selected row image NSDictionary* currentImageDictionary=[self.topfifty objectAtIndex:indexPath.row];//topFifty is an array of image dictionaries UIImage *currentImage = [currentImageDictionary objectForKey:@"image"]; if (currentImage) { // we already fetched the image, so we just set it to the cell's image view cell.imageView.image = currentImage; } else { // we don't have the image, so we need to fetch it from the server // In the meantime, we can set some place holder image UIImage *palceholderImage = [UIImage imageNamed:@"placeholder.png"]; cell.imageView.image = palceholderImage; // set the placeholder as the current image to your model, so you won't // download the image multiple times (can happen if you reload this cell while // download is in progress) [currentImageDictionary setObject:palceholderImage forKey:@"image"]; // then download the image // creating the download queue dispatch_queue_t downloadQueue=dispatch_queue_create("thumbnailImage", NULL); dispatch_async(downloadQueue, ^{ UIImage *downloadedThumbImage=[self getImage:currentImageDictionary] ; //Need to go back to the main thread since this is UI related dispatch_async(dispatch_get_main_queue(), ^{ // store the downloaded image in your model [currentImageDictionary setObject:image forKey:@"image"]; // update UI UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.imageView.image = image; }); }); dispatch_release(downloadQueue); } return cell; }