我正在使用 TBXML+HTTP 从网站获取 XML 数据。我想要做的是用处理后的数据填充 UITableView。我创建了一个包含所有条目的 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:
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 行,并且延迟完全相同。