UITableView calls numberOfRowsInSection multiple times, why?

Hello - During my App's startup as with most any App with a UITableView with Sections, numberOfSections is called -

   func numberOfSections(in tableView: UITableView) -> Int {
   return fetchedResultsController.sections?.count ?? 0
  }

as you can see, the number provided by the fetchResultsController is returned, which in my current case, returns 81.

Then

   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int

is called once for each of the 81 sections, OK, I'm not sure why it needs all 81 counts at once, perhaps the first couple screen fulls would suffice, but then, it's called again, and again, and again until numberOfRowsInSection has been called 480 times. That seems a bit excessive, wouldn't you agree?

Meanwhile, viewForHeaderInSection has been called 16 times for the 4 sections currently visible.

This can't be working as designed, can it?

Is there some reason UITableView can't rely on the answers it receives the first time?

I've searched and tried every solution (suggestion) I've found on StackOverflow to no avail.

Obviously I'm missing something, maybe a well hidden delegate call that should be answered to thwart this repeated cycle of insanity - tableView apparently thinks doing the same thing over and over will yield different results.

Hope you can help, thank you for your time.

perhaps the first couple screen fulls would suffice

That's not how the system was designed. There may be many reasons behind the scene why it is needed. It avoids having to keep track of what was evaluated and not.

That seems a bit excessive, wouldn't you agree?

No.

UITableView can't rely on the answers it receives the first time?

Are you so sure nothing may have change between call ?

What is the problem, for you ? You should not care about optimizing system calls.

Thanks for your response, however, I’m looking for constructive information that may lead to resolving why UITableView makes the same calls to the delegate over and over.

Like I stated, my guess is my App must not be as answering some call that I’m not aware of - I was hoping to gain that insight here.

I understand the curiosity to see under the hood, but that's an iOS design choice. And it is not documented and may even change in the future.

The point is however: what is the issue ? Does it have any bad side effect on your app ?

.

my guess is my App must not be as answering some call that I’m not aware of 

Why do you guess so ?

Thanks for your response, however, my “curiosity” is only one of App performance, not any “under the hood” revelations.

Of the Apps containing a UITableView I’ve created, I’ve never before experienced or observed such errant behavior.

I was hoping my post would be seen by a fellow developer that has encountered this issue before that could share the cause and their solution.

I should have included in my original post -

If you’re a developer that has encountered this or similar errant behavior implementing a TableView from a large data source with many sections and has resolved it, I’d love to hear from you as to how you resolved the issue. Thank you in advance.

So you have noticed a performance issue in your app?

I do not see what is "errant behavior" from what you describe. May be unexpected, but what is errant ?

The question has been raised several times, with a similar answer to mine, such as here:

https://stackoverflow.com/questions/36224842/why-is-the-numberofrowsinsection-being-called-multiple-times-for-only-one-sectio

or here:

https://developer.apple.com/forums/thread/21444

Feel free to send a bug report or enhancement request if that causes a performance issue.

If you feel that this is an issue that could be improved you are always welcome to file a Feedback report. However consider that it is likely that UIKit has worked like this for a long time, and there are a lot of clients that may be (directly or indirectly) depending on any number of behaviors like this that may be the reason why such behavior persists.

In particular however, UIKit does expect these methods to be very fast because they may need to be called during other calculations that also need to be very fast. And while it is hard to deny that calling something once and remembering the value can be faster than calling it twice, it can also be very hard to do so in certain cases.

Slightly OT, but if your minimum platform version and architecture allow it, I’d highly recommend migrating to diffable data sources: UITableViewDiffableDataSource and friends. It can make things a lot easier and you’ll never have to implement (or even think about) those boilerplate delegate methods again.

UITableView calls numberOfRowsInSection multiple times, why?
 
 
Q