UITableView insertRowsAtIndexPaths:WithRowAnimation没有冻结UI [英] UITableView insertRowsAtIndexPaths:WithRowAnimation without freeze UI
问题描述
我尝试了解当我使用 UITableView
时要使用的最佳实践,当表可见时要插入大量行。
I try to understand what is the best practice to use when I work with UITableView
with large number of row to insert when the table is visible.
这是我的行为:
我有一个 UITableView
和一个尝试将数据插入此表的线程。我认为制作 [UITableView reloadData]
对于性能方面来说是一个糟糕的解决方案,我知道 UIKit
操作在主线程上进行,因此在数据源更新完成时我尝试发送 NSNotification
来生成 UITableView
在主线程上更新操作(beginUpdate - insertRowAtIndex - endUpdate),但这种技术会冻结用户界面。我必须处理1000多行。
I have one UITableView
and one Thread that try to insert a data into this table. I think that make a [UITableView reloadData]
is a poor solution for the performance aspect, and I know that UIKit
operation are be carried on main thread, for this reason when the datasource update is completed I tried to send a NSNotification
to make a UITableView
update operation (beginUpdate - insertRowAtIndex - endUpdate) on the main thread, but this technique freeze the user interface. I have to work with 1000+ number of rows.
有人已经解决了这个问题吗?是否可以使用GDC异步和同步?如果有,怎么样?
提前致谢。
Someone has already solved this problem ? Is it a possible solution the use of GDC async and sync? If yes, how? Thanks in advance.
推荐答案
您是否尝试这样:
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView beginUpdates];
[self.tableDatalist insertObject:obj atIndex:index];
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]]
withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];
});
这篇关于UITableView insertRowsAtIndexPaths:WithRowAnimation没有冻结UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!