Swift — 4 — Core Data — Part 6B Using NSFetchResultController (Section Title) with UITableView

In previous post we talk about NSFetchedResultsController (NSFRC) basics, it’s delegate methods etc. In this post we will see how we can show section titles using NSFRC. Final output will look like this:

Please support us by comment, like and share my facebook page.

Download starter project from here

Let’s say we want to make person’s Name as section title of our table view sections using NSFetchedResultsController class. We have a property named sectionNameKeyPath which can be used for this purpose.

Let’s make minor changes in fetchedResultsController lazy initialization:

Only change in fetchedResultsController initialization is sectionNameKeyPath. I have used “name” property/attribute’s value of Person class to be the section title of our table view.

We also need to implement two delegate methods numberOfSections & titleForHeaderInSection :

Last change we need to make is in controllerDidChangeContent delegate method to update table view data:

We have already talked about insert rows and delete rows. Similarly we have section specific insert and delete operation for inserting or deleting at particular section.

Build and run your project and now you should be able to show section titles also.

Thanks for reading.

Happy Coding!

Please support us by comment, like and share my facebook page.

Final source code is here  😉 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.