我的 UITableView
中有大约 50 个自定义单元格。我想在从 URL 获取图像的单元格中显示图像和标签。
我想延迟加载图像,以便在加载图像时 UI 不会卡住。我尝试在单独的线程中获取图像,但每次单元格再次可见时我都必须加载每个图像(否则重用单元格会显示旧图像)。有人可以告诉我如何复制这种行为。
最佳答案
试试AFNetworking类 在这个链接下载这个类https://github.com/AFNetworking/AFNetworking .在您的项目中添加所有 AFNetworking 类。然后只需导入此类别
#import "UIImageView+AFNetworking.h" in your Viewcontroller which contains your Tableview.
然后在 cellForRowAtIndexPath: 中放如下
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil)
{
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
[cell.imageView setImageWithURL:[NSURL URLWithString:[UrlArray objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
cell.myLabel.text = [imageNameArray objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
这会异步下载 UITableviewcell 中的 imageView 的图像。当用户滚动 Tableview 时,它不会一次又一次地下载,因为它也有缓存。下载图像后,它会使用 imageUrl 的 key 保存图像。希望对你有用。
关于iphone - 在 UITableView 中延迟加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1130089/