ios - 长时间重新加载 UITableView

标签 ios uitableview reloaddata

我有一个问题, 当我加载表时我有空数据源,之后当我从服务器获取数据时 我这样做[tableview reloadData] 但是我在 numberOfRowsInSectionCellForRowAtIndexPath 之间有很大的延迟,所以这次我的表看起来卡住了。 有没有人可以在这里帮助我!

2014-05-20 14:07:02.803[8097:4f03] didReceivePlaces
2014-05-20 14:07:02.809[8097:4f03] numberOfRowsInSection
2014-05-20 14:07:13.310[8097:4f03] cellForRowAtIndexPath
2014-05-20 14:07:13.352[8097:4f03] cellForRowAtIndexPath
2014-05-20 14:07:13.368[8097:4f03] cellForRowAtIndexPath
2014-05-20 14:07:13.383[8097:4f03] cellForRowAtIndexPath
2014-05-20 14:07:13.397[8097:4f03] cellForRowAtIndexPath

编辑 我找到了我需要像这样在主线程中重新加载表的答案,它工作得很好! [表 performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];

最佳答案

在向您的 Web 服务发送 URLRequest 时,您的 UI 会被阻止,直到收到响应为止。解决方案是在单独的线程上运行您的请求,这样它就不会阻塞您的 UI。

示例:

NSString *urlStr = @"http://www/yourwebsite.com/webservice/";
NSString *postString = @"username=Username&pass=123545";
NSData *postData = [postString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL:[NSURL urlQithString:urlStr];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

// create NSOperationQueue for your webservice call
NSOperationQueue *queue = [[NSOperationQueue alloc]init];

// rnu your webservice call on the separate operation queue with completion block
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError){

// do whatever you have to do when webservice returns data


//update your tableview on the main thread at the end
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [tableView reloadData];
    }];

}];

关于ios - 长时间重新加载 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23756954/

相关文章:

uitableview - 调用 self.tableView.reloadData 需要 10-15 秒

ios - Sprite Kit-施加静态脉冲

ios - 我将如何在 Swift 中重写这个 Objective-C 策略模式?

ios - 第一次未设置 UITableview 单元格标签字体大小

ios - 关于iPhone切换应用时更改状态栏颜色的问题

ios - 重新排列 UITableView 中的行;保存到核心数据

ios - 如何在关闭 UIViewController 后重新加载 tableView

ios - 是否有与 mkmapsnapshotter 等效的 Google map ?

ios - UICollectionview 在滚动时更改选择/禁用选择 - iOS

ios - 如何在选项卡栏 Controller 中更新选项卡 View