【问题标题】:Update UiTableview with data getting from url使用从 url 获取的数据更新 UiTableview
【发布时间】:2016-12-04 08:38:24
【问题描述】:

在我的应用程序中,我正在实现 UITableView,并从 URL 获取数据。

一切正常。但这里的问题是来自后端 url 的数据太大。所以我显示UIActivityIndicatorView,直到数据获取完成。

由于数据量很大,提取数据通常需要 3 或 4 分钟。那么如何尽快用从 url 获取的数据更新UITableView

我正在使用asynchronousRequest 来做这件事。

我在cellForRowAtIndexPath使用了这段代码

NSString *post = [NSString stringWithFormat:@"skey=%@&user_id=%@",@"XXXXXX",@"3225"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://chkdin.com/dev/api/peoplearoundmexy/?%@",post]]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:nil];

NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    if (data) {
        id json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
        NSMutableArray *designation=[json valueForKey:@"designation"];
        UIImage *imageobj1=[UIImage imageNamed:@"userpic.jpg"];
        NSData *imagedata = UIImagePNGRepresentation(imageobj1);

            dispatch_async(dispatch_get_main_queue(), ^{
                PeopleNearbyCell *updateCell = (id)[collectionview cellForItemAtIndexPath:indexPath];
                if (updateCell)

                    cell.UserProfilePic.image = [UIImage imageWithData:imagedata];
            });
    }
}];
[task resume]; 

但它不起作用。我怎样才能做到这一点?

【问题讨论】:

  • 我不会在cellForRowAtIndexPath 中执行请求,而是在viewDidLoad 中执行请求。我相信cellForRowAtIndexPath 会在需要新单元格时被调用,无论是第一次用单元格填充屏幕还是将新单元格滚动到屏幕中。
  • 即使我在视图中点击服务器也确实加载了,花费太多时间来获取数据。我不想等到数据完全加载,我不断从 url 获取数据我必须更新 tableview
  • 您是否尝试过减少您尝试请求的数据量?我知道有些端点可以选择。
  • 不,我没试过,请告诉我
  • 您使用的是什么 API? @SivaSankar

标签: ios objective-c iphone xcode uitableview


【解决方案1】:

网络请求是异步处理的,cellforrowatindexpath在主线程中,如果需要在网络请求中刷新表,需要编写一个网络请求方法,然后获取数据,其中方法赋值,然后刷新表格。

【讨论】:

    【解决方案2】:

    您可以使用lazyload 方法。此代码在 cellForRow 委托中。

    [cell.loadActivity startAnimating];
    
        dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
            dispatch_async(backgroundQueue, ^{
    
              // YOUR LOAD FROM URL METHOD HERE.
    
                // only update UI on the main thread
                dispatch_async(dispatch_get_main_queue(), ^{
    
    
                    if (img==nil) {
    
                        // HANDLE WHEN IMAGE RETRIEVE FAILED
                    } else {
    
                       // ASSIGN IT
                        cell.UserProfilePic.image = img;
                    }
    
                    [cell.loadActivity stopAnimating];
    
                });
    
            });
    

    【讨论】:

      猜你喜欢
      • 2016-07-18
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 2021-10-13
      • 2021-04-08
      • 2013-06-19
      • 1970-01-01
      相关资源
      最近更新 更多