【发布时间】:2015-08-18 10:19:22
【问题描述】:
我有一个使用自定义单元格的TableView。我最初是在 cellForRowAtIndexPath 方法中设置从 URL 抓取图像
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSDictionary *dictObject = [places objectAtIndex:indexPath.row];
cell.nameLabel.text = [dictObject valueForKey:@"PlaceTitle"];
NSURL *url = [NSURL URLWithString:@"http://images1.fanpop.com/images/image_uploads/Mario-Kart-Wii-Items-mario-kart-1116309_600_600.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
cell.thumbnailImageView.image = image;
return cell;
}
但这让我的 TableView 滚动变得迟钝。一旦我删除了图像提取,它滚动正常,所以我知道这是我的问题。
我的问题是:如何异步获取此图像并将其设置在我的单元格中?是否有捷径可寻?谢谢!
【问题讨论】:
-
看看
NSURLSession、NSURLConnection和朋友们。 -
您查看过 sd_webimage 吗?它将负责下载和设置图像以及缓存.. github.com/rs/SDWebImage
-
这可能对您有所帮助...MJTableImageSwift。将其更改为目标 C。
标签: ios image asynchronous tableview