UITableViewDataSource: titleForHeaderInSection llamado para secciones invisibles

I want to display a very long list elements, divided into approximately 580 sections. When I open the UITableViewController it takes a second or two (on a iPhone 5) to load the controller. From instrumenting the code I learned that it is the string operations I do for every section's title that delays the display of the table. If returning a constant string it performs way faster.

  • Question 1: Why does the UITableView construct all section headers in advance when on the other hand it tries to reduce load by rendering cells lazily?
  • Question 2: What can I do about it? Is there a way to tell the UITableView not to do so or would you store the titles in Core Data themselves? I display dates as section titles, so it may not be wise to store them as strings as they depend on the location, daylight saving time, language, etc.

preguntado el 15 de noviembre de 13 a las 08:11

1 Respuestas

From your question ("store titles in core data?") I gather that you are not storing the section titles in Core Data. Presumably you are using some array in memory etc. so regardless of what the table view datasource methods are, you will have to compute all 500 strings.

Instead you should be using the fully optimised NSFetchedResultsController. Give it an appropriate sectionNameKeyPath when creating it, and you will find that your table view will load in a snap.

respondido 15 nov., 13:10

Yo uso un NSFetchedResultsController, I was just wondering why the UITableView llamadas titleForHeaderInSection: even for the invisible headers when it doesn't do it for cells. - Paul

En cuanto a la sectionNameKeyPath: This would solve the current issue at the expense of having to keep the section names up to date, as they depend on the location and other factors. Thanks anyway. - Paul

I do not see your point. You have to compute the titles anyway, so it would still be more efficient to store them in Core Data. With fetched properties you can automate the updates. - Mundi

I probably never have to computer more than a few titles to computer. The remaining data is just there, but it's not relevant most cases. Sorry for not making this clearer. - Paul

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.