我有一个带有内容的UITableView
,这些内容是以异步方式加载的。当用户旋转设备时,我需要在-willRotateToInterfaceOrientation
方法中进行[tableView reloadData]
操作。在我的情况下,ReloadData是异步工作的。
我知道reloadData在主线程中工作,但它会触发cellForRowAtIndexPath,在我的情况下也是异步的。
所以问题是如何使主线程等待UITableView的reloadData
结束。
我有一个带有内容的UITableView
,这些内容是以异步方式加载的。当用户旋转设备时,我需要在-willRotateToInterfaceOrientation
方法中进行[tableView reloadData]
操作。在我的情况下,ReloadData是异步工作的。
我知道reloadData在主线程中工作,但它会触发cellForRowAtIndexPath,在我的情况下也是异步的。
所以问题是如何使主线程等待UITableView的reloadData
结束。
您需要在后台加载表格数据,然后调用UITableView的reloadData方法。
您可以使用GCD轻松地将加载函数异步分派到后台队列。当该任务完成时,工作线程将返回主线程一个块来调用[tableView reloadData]
。以下是方法:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
...
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
// load your table data here
[self loadMyTableData];
// when done dispatch back to the main queue to load the table
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue, ^{
[self.tableView reloadData];
});
});
...
}