ios - UITableView 延迟加载的图像在手指关闭之前不会加载

标签 ios objective-c uitableview asynchronous lazy-loading

我有一个应用程序可以加载很多大图像。当我延迟加载它们时,甚至在加载图像之后,单元格也不会加载它们,直到我将手指从屏幕上移开。我在 UIScrollViewDelegate 方法 scrollViewDidEndDraggingscrollViewDidEndDecelerating 中调用我的 downloadImageForVisiblePaths 函数,除此之外,我还设置UITableViewcellForRowAtIndexPath 方法中的图像如下所示:

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

//     Code to load reusable custom cell

CustomObject *object = (CustomObject*) [self.tableArray objectAtIndex: indexPath];

if(!object.mainImage){

    [self downloadImageForIndexPath:indexPath];

    cell.mainImageView.image = [UIImage imageNamed:@"placeholder"];

}else{

    cell.mainImageView.image = object.mainImage;

}

    return cell;
}

downloadImageForIndexPath 如下所示:

-(void) downloadImageForIndexPath:(NSIndexPath*) indexPath{

    UIImage *loadedImage = [[UIImage alloc] init];

    // take url and download image with dispatch_async

    // Once the load is done, the following is done

    CustomCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    cell.mainImageView.image = loadedImage;

    CustomObject *object = (CustomObject*) [self.tableArray objectAtIndex: indexPath];

    object.mainImage = loadedImage;

    dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableVIew reloadData];
        });
}

我看不出哪里错了。即使手指在屏幕上,我也需要加载图像。这种行为类似于图像在 Google+、Instagram 或 Facebook 等应用程序上的加载方式。

任何指点将不胜感激。

最佳答案

这很难说,因为您没有包含 downloadImageForIndexPath 的所有代码,但看起来您正在从后台线程将图像分配给单元格(您不应该触摸 UI 控件来自后台线程)。此外,如果您直接更新单元格,则无需调用 reloadData

我还建议使用 SDWebImage用于在表格 View 中显示远程图像。

关于ios - UITableView 延迟加载的图像在手指关闭之前不会加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19093349/

相关文章:

ios - 像Skype应用程序一样,如何在前台状态下进行通话时显示顶部栏状态栏

objective-c - 如何修复 Swift 中桥接 header 的错误?

ios - 一个类似于 UIImagePickerController 的 Picker Controller 来选择文件 ios 7

ios - 如何在重新排序期间更新 UITableViewCell

ios - Crashlytics(无 Fabric)未显示崩溃 iOS

ios - 通过 iOS App 销售 iBooks

ios - 当其他文本字段 ios 出现弹出窗口时如何关闭已经打开的键盘

ios - 防止 UITableViewCell.backgroundView 在方向更改时调整图像大小

ios - 如何调整图像大小以适应 tableview 单元格中的 UIImageview

iOS - UIToolBar 作为 UITextView 的 inputAccessoryView