我有tableviewcontroller和从服务器感染的数据。我使用以下 class 下载异步数据。但是我的问题是当用户看到tableViewcontroller时正在加载数据。我希望在用户看到之前加载数据。

    #import <SDWebImage/UIImageView+WebCache.h>

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

    cell.textLabel.text = [[tableData objectAtIndex:indexPath.row] valueForKey:@"name"];
    cell.textLabel.font = [UIFont fontWithName:@"BebasNeue" size:24];
    cell.textLabel.textColor = [UIColor whiteColor];


    NSString *imageURLString=[[tableData objectAtIndex:indexPath.row] valueForKey:@"logo"];
    NSString* imageURL = [[tableData objectAtIndex:indexPath.row] valueForKey:@"picture"];
    [cell.imageView setImageWithURL:[NSURL URLWithString:imageURL]];

}

最佳答案



使用SDWebImage,您可以先下载图像。

哪里?

这取决于您的实现。可能在以前的控制器中,或者在appDelegate中。如果tableview出现在您的初始viewcontroller中,则机会完全取决于网络速度。

范例程式码

看一下这段代码(摘自该库的自述文件,请检查源代码!):

Manager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:imageURL
                 options:0
                 progress:^(NSInteger receivedSize, NSInteger expectedSize)
                 {
                     // YOU DON'T NEED THIS
                 }
                 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
                 {
                     if (image)
                     {
                         // MAYBE YOU WANT TO DO SOMETHING HERE
                     }
                 }];

然后,在tableView:cellForRowAtIndexPath:方法中,您可以像这样设置图像:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // ...
    NSURL* url = GET_IMAGE_URL;
    [cell.imageView setImageWithURL:url];
    // ...
}

怎么运行的?

该库负责先在其缓存中查找,然后在其中查找先前下载的图像,因为该库将图像URL用作缓存的键,因此该图像会间接显示。

关于ios - 首先下载数据,然后在TableViewcontroller上显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23742210/

10-12 03:17
查看更多