Ios 长时间重新加载UITableView

Ios 长时间重新加载UITableView,ios,uitableview,reloaddata,Ios,Uitableview,Reloaddata,我有个问题, 当我加载表时,我有空的数据源,之后当我从服务器获取数据时 我这样做[tableview重载数据] 但是我有一个很大的延迟,就是numberOfRowsInSection和cellforrowtaindexpath,所以我的表在这个时候看起来冻结了。 有人能帮我吗 2014-05-20 14:07:02.803[8097:4f03] didReceivePlaces 2014-05-20 14:07:02.809[8097:4f03] numberOfRowsInSection 20

我有个问题, 当我加载表时,我有空的数据源,之后当我从服务器获取数据时 我这样做
[tableview重载数据]
但是我有一个很大的延迟,就是
numberOfRowsInSection
cellforrowtaindexpath
,所以我的表在这个时候看起来冻结了。 有人能帮我吗

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)with object:nil waitUntilDone:YES]

尝试在主线程上重新加载UITableView。UI更新应始终在主线程上完成。

dispatch_async(dispatch_get_main_queue(), ^{
                    [tableview reloadData];
        })

URLRequest
发送到web服务时,用户界面将被阻止,直到收到响应为止。解决方案是在一个单独的线程上运行您的请求,这样它就不会阻塞您的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];
    }];

}];

您添加的日志消息不足以缩小问题所在的范围。请告诉我们您在哪里呼叫
[tableView reloadData]
。看起来你在主线程上运行的另一个任务阻塞了你的UI。是的,你是对的!我同时使用服务器,所以您可以帮助我找到使用[table performSelectorOnMainThread:@selector(reloadData)with Object:nil waitUntilDone:YES]的正确方法;所有UI更新都在主线程上执行。如果您尝试在后台更新任何UI,您将收到一个异常。唯一的解决办法是在后台运行webservice请求,当您收到响应时,只需在主线程上更新您的
tableView
。我马上给你举个例子。