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