Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what order are UITableView hook methods called?

In what order are the following methods called for a UITableView?

  • numberOfSectionsInTableView
  • numberOfRowsInSection
  • cellForRowAtIndexPath
  • heightForRowAtIndexPath
  • didChangeObject (NSFetchedResultsController)

Please include any other pertinent hook methods that I may have left out.

like image 284
ma11hew28 Avatar asked Apr 06 '11 23:04

ma11hew28


2 Answers

I would be wary about being concerned with the order in which these methods are called. These methods have very specific, and complete purposes. Even though the methods are called in a certain order right now, that doesn't mean they will always be called in that order so it is dangerous to make assumptions in your code as to what order they are called. For example, cellForRowAtIndexPath is not called for all rows. It is only called for visible rows and then called for additional rows as the user scrolls to them. Old rows are also destroyed as they go off the screen and it will request the cells again if the user scrolls back up.

Bottom line, since the order is not specified in Apple's documentation, you cannot safely assume that they will always be called in the same order and it is generally not a good idea to make such assumptions in your implementation.

like image 141
drewag Avatar answered Sep 24 '22 02:09

drewag


I eventually did use NSLog statements to figure this out but unfortunately didn't document it. For, I realized that the order those functions are called didn't matter.

Reading about Batch Insertion, Deletion, and Reloading of Rows and Sections in the Table View Programming Guide for iOS I learned to update the data before inserting rows in the table view.

like image 44
ma11hew28 Avatar answered Sep 23 '22 02:09

ma11hew28