Question

This is the situation. I have a table with about 20 sections ( user can add more ) each section can have many items (depends on the user). I have implemented a Section collapse - expand scenario and works well. I have a custom Section Header View With a button to toggle open and close states. I am using NSFetchRequest to get all the sections from Core Data (initial state Closed only sections are shown). Every time the user clicks on the header button I fetch with a predicate the items that belong to that section, Section opens Cells animate in place and everything works well.

I was wondering is there a better way of doing this with core data, any ideas for something more elegant will be highly appreciated.

==================================== EDIT ================================== MARCUS SUGGESTION WAS 10 TIMES BETTER THAN MINE. THANK YOU MARCUS

Was it helpful?

Solution

I would suggest having one NSFetchedResultsController and keep the data live. Then have a state array for the sections to know whether they are open or closed. From there you can answer the "number of rows" question by first checking state and then if the state is open asking the NSFetchedResultsController how many items are actually in that section and replying with the answer.

When the user taps on a section you can then tell the table to add the cells via animation, etc. and remove them on a close. No reason to fetch each section individually or repeatedly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top