在表格视图单元格中显示来自图像URL的UIImageView

时间:2014-03-26 18:21:25

标签: multithreading uiimageview nsurl

我想在桌面视图的单元格中显示图像。我为表创建了自定义单元格,每个单元格都有一个UIImageView。

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellIdentifier = @"autocompletelistcell";

    Data *data=[datas objectAtIndex:indexPath.row];
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    UIImage *image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://domain.com/image.jpg"]]];
    cell.imageview.image=image;
    cell.name=data.name;

    return cell;
}

但是加载所有图像需要很长时间(实际上只有5张图像但尺寸不同),并且在加载所有图像之前不会显示UITableView。

在完成图像加载之前是否可以显示表格和单元格?我想我需要多线程?

1 个答案:

答案 0 :(得分:0)

我会考虑使用AFNetworking libraries,尤其是UIImageView + AFNetworking。

然后你可以做这样的事情并让图像加载异步......

[cell.imageview setImageWithURL:[NSURL URLWithString:@"http://domain.com/image.jpg"
                               placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
相关问题