19

When you call -reloadData on a table view instance, does it make all UITableViewDataSource calls async? Thank you.

Alexsander Akers
  • 15,967
  • 12
  • 58
  • 83
the Reverend
  • 12,305
  • 10
  • 66
  • 121
  • Make sure you see [How to tell when UITableView has completed ReloadData?](https://stackoverflow.com/questions/16071503/how-to-tell-when-uitableview-has-completed-reloaddata) – mfaani Nov 22 '19 at 21:47

1 Answers1

20

This method actually just removes all the UITableViewCell views from the table. The data source delegate methods are called when the table is repainted.

So, it's asynchronous.

Edit:

Actually, some calls ARE synchronous. The number of sections & rows and row heights are updated immediately, so, for example contentSize is set correctly after the call. On the other hand, tableView:cellForRowAtIndex: is not called until the table is repainted.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • 1
    so if i had an array backing the tableview. cellForRowAtIndexPath would crash if the array is updated before it gets called, how should we fix this? – bogardon Oct 17 '12 at 21:38
  • @bogardon I don't understand - why would it crash? – Sulthan Oct 18 '12 at 09:37
  • 4
    It crashes if reloadData is called again before all the `cellForRow...` calls are made and the new number of cells < old number of cells. `cellForRow` asks for too high of an index – bendytree May 23 '13 at 23:50
  • At least in the case of collection views on iOS 8.2+, numberOfItemsInSection is also called asynchronously, driven from the layout code (flow layout etc). – Chris Conover Mar 19 '15 at 22:25
  • @bendytree so do I have to prevent such situation? – Alexander Perechnev Jul 18 '15 at 21:38
  • 1
    You don't have to. When you reload the data, it will check number of sections and items per section before calling cellForRowAt. So if you call reloadData after updating your array it'll work as expected. However, if you silently change your array and not call reloadData, it will crash. – ravindu1024 Jan 06 '20 at 23:31