Lazy load images in UITableView(在 UITableView 中延迟加载图像)
问题描述
我的 UITableView
中有大约 50 个自定义单元格.我想在从 URL 获取图像的单元格中显示图像和标签.
I have some 50 custom cells in my UITableView
. I want to display an image and a label in the cells where I get the images from URLs.
我想延迟加载图像,以便在加载图像时 UI 不会冻结.我尝试在单独的线程中获取图像,但每次单元格再次可见时我都必须加载每个图像(否则重用单元格会显示旧图像).有人可以告诉我如何复制这种行为.
I want to do a lazy load of images so the UI does not freeze up while the images are being loaded. I tried getting the images in separate threads but I have to load each image every time a cell becomes visible again (Otherwise reuse of cells shows old images). Can someone please tell me how to duplicate this behavior.
推荐答案
试试AFNetworking类 在这个链接下载这个类https://github.com/AFNetworking/AFNetworking.在您的项目中添加所有 AFNetworking 类.然后只需导入此类别
Try AFNetworking class download this class in this link https://github.com/AFNetworking/AFNetworking . Add the all AFNetworking class in your project.Then just import this category
#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 的密钥保存图像.希望对你有用.
This download your Image for imageView in the UITableviewcell asynchronously. It wont download again and again while the user scroll the Tableview, because it has cache also. Once your image download it save the image with key of your imageUrl. I hope it useful for you.
这篇关于在 UITableView 中延迟加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 UITableView 中延迟加载图像


基础教程推荐
- NSString intValue 不能用于检索电话号码 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01