UITableView reloadData需要太多的时间

我正在使用TBXML + HTTP从网站获取XML数据。 我想要做的是用处理的数据填充一个UITableView。 我创build了一个包含所有条目的NSMutableArray,就这一点而言,一切工作正常。 问题是,当成功从服务器获取数据并将其存储在数组中后,我尝试使用reloadData更新表以显示数据。

用20行填充表格大约需要5秒钟的时间。 怪异的部分是抓取发生得非常快,所以数据立即可用。 我不明白这是多久了。 以下是日志中的一些信息:

 2012-03-17 18:46:01.045 MakeMyApp[4571:207] numberOfSectionsInTableView: 1 2012-03-17 18:46:01.047 MakeMyApp[4571:207] numberOfRowsInSection: 0 2012-03-17 18:46:01.244 MakeMyApp[4571:1f03] numberOfSectionsInTableView: 1 2012-03-17 18:46:01.245 MakeMyApp[4571:1f03] numberOfRowsInSection: 20 2012-03-17 18:46:01.245 MakeMyApp[4571:1f03] Ok, I'm done. 20 objects in the array. 2012-03-17 18:46:01.246 MakeMyApp[4571:1f03] Finished XML processing. 2012-03-17 18:46:06.197 MakeMyApp[4571:1f03] cellForRowAtIndexPath: 

正如你所看到的,它激发了numberOfSectionsInTableView: / numberOfRowsInSection:两次:第一次是当视图加载时,第二次是当我做[self.tableView reloadData];

您会看到,在不到一秒钟的时间内,该数组将填充20个对象,并且所有处理XML的工作都已完成。 cellForRowAtIndexPath:如何cellForRowAtIndexPath:仅在5秒后触发?

以下是可能有助于发现问题的部分代码:

 - (void)createRequestFromXMLElement:(TBXMLElement *)element { // Let's extract all the information of the request int requestId = [[TBXML valueOfAttributeNamed:@"i" forElement:element] intValue]; TBXMLElement *descriptionElement = [TBXML childElementNamed:@"d" parentElement:element]; NSString *description = [TBXML textForElement:descriptionElement]; TBXMLElement *statusElement = [TBXML childElementNamed:@"s" parentElement:element]; int status = [[TBXML textForElement:statusElement] intValue]; TBXMLElement *votesCountElement = [TBXML childElementNamed:@"v" parentElement:element]; int votesCount = [[TBXML textForElement:votesCountElement] intValue]; // Creating the Request custom object Request *request = [[Request alloc] init]; request.requestId = requestId; request.description = description; request.status = status; request.votes_count = votesCount; [requestsArray addObject:request]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { appListCell *cell = (appListCell *)[tableView dequeueReusableCellWithIdentifier:@"appListCell"]; Request *request = [requestsArray objectAtIndex:indexPath.row]; cell.appIdLabel.text = [NSString stringWithFormat:@"#%d", request.requestId]; cell.appTextLabel.text = request.description; cell.appVotesLabel.text = [NSString stringWithFormat:@"%d votes", request.votes_count]; return cell; } 

非常感谢!

编辑:

 - (void)traverseXMLAppRequests:(TBXMLElement *)element { int requestCount = 0; TBXMLElement *requestElement = element->firstChild; // Do we have elements inside <re>? if ((requestElement = (element->firstChild))) { while (requestElement) { [self createRequestFromXMLElement:requestElement]; requestCount++; requestElement = [TBXML nextSiblingNamed:@"r" searchFromElement:requestElement]; } } [self.tableView reloadData]; NSLog(@"Ok, I'm done. %d objects in the array.", [requestsArray count]); } 

编辑2:我试图提取只有1行,而不是20的信息,延迟是完全一样的。

我知道这是一个非常古老的问题,但我刚刚遇到同样的问题,并认为我可能知道原因。 可能帮助那些在同一个问题上绊倒的人

我怀疑你的reloadData调用发生在后台线程上,而不是主线程。 你所描述的是这个的确切效果 – 代码运行,但是更新用户界面有一个很长的延迟(5-10秒)。

尝试将reloadData调用更改为:

 dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; }); 

您在mainThread以外的线程上调用reloadData

请确保在主线程上重新加载tableView的数据,如下所示:

OBJ-C:

 [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; 

Swift 3:

 DispatchQueue.main.async { self.tableView.reloadData() } 

哪里有:

 - (void)createRequestFromXMLElement:(TBXMLElement *)element 

被叫? 听起来像你可能正在做20个Web请求来获取该文件的内容。 这肯定会让事情变得缓慢(而不是做一个请求来获取整个XML文件并在本地进行parsing)。

你可以发布你的整个VC代码?