繁体   English   中英

为什么我的[tableview reloadData]执行得太晚了?

[英]Why does my [tableview reloadData] execute too late?

更改数据时,我的tableview发生了一些奇怪的事情。 我有这样的事情:

// fadeout existing data
... // change data (new values for the cells are blank)
for{ // loop all rows
[TableView reloadRowsAtIndexPaths: withRowAnimation:]; // smooth fade out
}

// add one new row to the table
... // change data (just add one new row but leave the cells empty)
[TableView reloadData] // reload all of the data (new row should be added)

... // change data (just add values to the existing cells)
for{ // loop all rows
[TableView reloadRowsAtIndexPaths: withRowAnimation:]; // smooth fade in
}

从理论上讲,至少我认为应该可行。 但是我有一些小故障,所以我在for循环和以下代码中添加了NSLogs:-( - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath所以我注意到我的[tableView reloadData]在第一行之后执行渐入渐出的循环!@#

我当时正在考虑在reloadData和衰落循环之间进行某种延迟,但是我不想强迫它工作,而是希望它能正常工作。

  • 有没有更好的方法来完成我要尝试的工作?
  • 我可以在表格底部动态添加一行而不调用'reloadData'方法吗?

谢谢。

是的,只需使用tableView insertRowsAtIndexPaths方法,该方法然后将调用您的dataSource来加载这些新行。

要在表末尾加载一行,您首先需要更新数据源以包括新行,然后使用以下命令生成索引路径:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[yourdata count]-1 inSection:0];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[tableView insertRowsAtIndexpaths:indexPaths withRowAnimation:whateveryouwant];

请记住,这将立即调用tableView:cellForRowAtIndexPath方法,因此将新行插入表中之前更新dataSource以包括多余的行,并请注意,指定的indexPath中的行索引与数据中新添加的项匹配资源。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM