[TableView reloadData] And runloop, tableviewreloaddata

Source: Internet
Author: User

[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];});}

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.