先下载数据然后在TableViewcontroller上展示 [英] Download data first then show it on TableViewcontroller

查看:19
本文介绍了先下载数据然后在TableViewcontroller上展示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有来自服务器的 tableviewcontroller 和数据.我使用以下类来下载数据异步.但我的问题是当用户看到 tableViewcontroller 时正在加载数据.我希望在用户看到之前加载数据.

I have tableviewcontroller and data fecthed from the server. I use following class to download the data asyn. but my problem is data is loading when user sees the tableViewcontroller. I want data being loaded before user sees.

    #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 中,则可能完全取决于网络速度.

It depends on your implementation. Maybe in a previous controller, or in your appDelegate. If the tableview appears in your initial viewcontroller, chances are to absolutely depend on the network speed.

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

Take a look at this code (extracted from the Readme of the library, check the source!):

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: 方法中,您可以像这样设置图像:

Then, in your tableView:cellForRowAtIndexPath: method, you can just set the image like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // ...
    NSURL* url = GET_IMAGE_URL;
    [cell.imageView setImageWithURL:url];
    // ...
}

它是如何工作的?

该库负责首先在其缓存中查找,它会在其中找到先前下载的图像,因为它使用图像 url 作为缓存的键,因此图像会立即出现.

How it works?

The library takes care of looking first in its cache, where it will find the previously downloaded image, because it uses the image urls as keys for the cache, so that the image appears inmediatly.

这篇关于先下载数据然后在TableViewcontroller上展示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆