gpt4 book ai didi

ios - UITableView reloadData 花费太多时间

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:48:54 24 4
gpt4 key购买 nike

我正在使用 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: 两次:第一次是在加载 View 时,第二次是在我执行 [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 行的信息,并且延迟完全相同。

最佳答案

我知道这是一个真正的老问题,但我刚刚遇到了完全相同的问题,我想我可能知道原因。可能会帮助偶然发现同样问题的人......

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

尝试将 reloadData 调用更改为:

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

关于ios - UITableView reloadData 花费太多时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9752872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com