iphone - 在 UITableView 中延迟加载图像

标签 iphone ios objective-c uitableview

我的 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/

相关文章:

iphone - UIViewController 可以包含多个 UIView 吗?

ios - 征求用户意见,用自定义的UIAlertView,最多能通过多少?

ios - [String : AnyObject] be an AnyObject, 但 [String : AnyObject? ] 怎么可能不行?

iphone - 使用 ASIHTTPRequest 的 UITableViewCell 中的 UIImageView 性能

iphone - iOS 5 实例变量

ios - 插入数据库在 iOS 中不起作用?

ios - 创建 iOS/OS X 跨平台类

objective-c - 是否有针对 Google 的 Objective-C 风格的 Uncrustify 配置?

iphone - 哪些工具提供 Xcode 调试器来查找逻辑错误?

ios - 无法将新的 xib 添加到 View Controller