[TableView reloadData] And runloop, tableviewreloaddata
If you need to obtain the cell and height of tableView immediately after [tableview reloadData], or you need to scroll tableview, executing code directly after reloadData will be problematic.
Breakpoint debugging feels that [tableview reloaddata] is an asynchronous operation, so that the following code will not be executed after it is executed.
As a result, I thought of multithreading, which is the simplest way to use animation to delay execution.
[UIView animateWithDuration:0.3 animations:^{ [self.collectionView reloadData]; } completion:^(BOOL finished) { [self.collectionView setContentOffset:offset animated:NO]; }];
The solution is to solve the problem, but I still find out why runloop is not an asynchronous problem after checking the information.
[Tableview reloaddata] needs to be executed in runloop after the current method is executed in runloop. The processing function is executed in a serial queue in runloop. However, the Code after [tableview reloaddata] requires the calculation result of [tableview reloaddata]. Therefore, the code after [tableview reloaddata] requires a delay in execution. Only the current method no longer occupies runloop, and [tableview reloaddata] can be executed in runloop. In this case, the delayed execution is long before [tableview reloaddata] In runloop. If the data in the table is very large and is not completed in a runloop cycle, operations on the tableview View data are required. Apple does not directly provide reloadData APIs. You can use the following method to delay the reloadData operation.
Method 1: layoutIfNeeded forcibly redraws and waits for completion.
[Self. tableView reloadData]; [self. tableView layoutIfNeeded]; // refresh complete
Method 2:
ReloadData will be executed in the main queue column, while dispatch_get_main_queue will wait for the opportunity until the main queue column is idle.
[Self. collectionView reloadData]; dispatch_async (dispatch_get_main_queue (), ^ {// refresh completed [self. collectionView setContentOffset: offset animated: NO];});}