我有一个应用程序可以加载很多大图像。当我延迟加载它们时,甚至在加载图像之后,单元格也不会加载它们,直到我将手指从屏幕上移开。我在 UIScrollViewDelegate
方法 scrollViewDidEndDragging
和 scrollViewDidEndDecelerating
中调用我的 downloadImageForVisiblePaths
函数,除此之外,我还设置UITableView
的 cellForRowAtIndexPath
方法中的图像如下所示:
- (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/