我试图从一个url加载图像。在cellForRowAtIndexPath中使用dispatch_async加载图像
以下是我的代码..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@""];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
dispatch_async (dispatch_get_main_queue(), ^{
NSData * storeImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:productImageArray[indexPath.row]]];
self.productImage.image = [UIImage imageWithData:storeImageData];
});
[cell.contentView addSubview:self.productImage];
return cell;
}
的问题是,
- UI冻结,直到图像加载。
- 只有最后一个单元格正在加载图像,但其余单元格不加载图像。
我该如何解决这个问题?
请参阅Apple的[LazyTableImages示例应用](https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009394)。 – rmaddy