ios – 图像未在表格视图中立即加载

我正在使用SDWeb Image并从新闻API中获取与新闻文章相关的图像.

问题是,在我开始在UITableView上滚动之前,屏幕上的单元格图像没有加载.一旦我滚过一个单元格,它就会离开屏幕,一旦我回到它,最终会加载Image.

这是我的(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath代码

if ([FeedLocal.images count] == 0) {
    [cell.imageView setimage:[UIImage imageNamed:@"e.png"]];
}
else {    
    Images *imageLocal = [FeedLocal.images objectAtIndex:0];
    Nsstring *imageURL = [Nsstring stringWithFormat:@"%@",imageLocal.url];
    NSLog(@"img url: %@",imageURL);

    // Here we use the new provided setimageWithURL: method to load the web image
    __weak UITableViewCell *wcell = cell;
    [cell.imageView setimageWithURL:[NSURL URLWithString:[Nsstring stringWithFormat:@"%@",imageURL]]
                   placeholderImage:[UIImage imageNamed:@"115_64.png"]
                          completed:^(UIImage *image,NSError *error,SDImageCacheType cacheType) {
                              if(image == nil) {
                                  [wcell.imageView setimage:[UIImage imageNamed:@"115_64.png"]];
                                   //];
                              }
                          }
    ];
}

知道为什么会这样吗?看起来当UITableView加载时,在滚动开始之前,图像不会被告知加载或其他东西?

任何建议都非常感谢,谢谢!

解决方法

我之前遇到过类似的问题,结果看到tableview中的图像正确下载.您面临的真正问题是刷新问题.下载每个图像时,必须刷新它才能在tableview中显示.在我的例子中,下载部分是在一个单独的文件中完成的,所以我使用NSNotificationCenter告诉tableview控制器类刷新它.您可以使用以下代码执行以下操作:
if ([FeedLocal.images count] == 0) {
    [cell.imageView setimage:[UIImage imageNamed:@"e.png"]];
}
else {    
    Images *imageLocal = [FeedLocal.images objectAtIndex:0];
    Nsstring *imageURL = [Nsstring stringWithFormat:@"%@",SDImageCacheType cacheType) {
                              if(image == nil) {
                                  [wcell.imageView setimage:[UIImage imageNamed:@"115_64.png"]];
                                  [[NSNotificationCenter defaultCenter] postNotificationName:@"ImageDownloaded" object:nil];
                                   //];
                              }
                          }
    ];
}

然后你可以使用它调用重载数据,如下所示:

- (void) imageDownloaded:(NSNotification *)notification{

    [self.tableView reloadData];
}

这样,您无需滚动即可查看图像,而是在下载后立即显示图像.

希望这可以帮助!

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...