NLFetchedResultsTable
is a small open-source class that helps you
quickly create and configure a UITableViewController
that is backed
by an NSFetchedResultsController
. This is code I use in a lot of my
projects, and have extracted into its own class. The primary benefit
of using an NLFetchedResultsTable
is that it handles a lot of the
NSFetchedResultsControllerDelegate
and UITableViewDataSource
methods
for you.
You can create and instantiate an NLFetchedResultsTable
on its own,
or you can subclass it to customize to your liking. In the sample
code, the app delegate creates an instance of RootViewController with
the following:
RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];
rootViewController.managedObjectContext = self.managedObjectContext;
rootViewController.entityName = @"Event";
rootViewController.cellTextLabelKey = @"name";
rootViewController.cellDetailTextLabelKey = @"timeStamp";
The RootViewController class subclasses NLFetchedResultsTable
to
provide its own add and edit records functionality.
After creating an NLFetchedResultsTable
or a subclass of NLFetchedResultsTable
,
the following properties are required to be set prior to displaying:
- managedObjectContext - the
NSManagedObjectContext
from which to fetch objects - entityName - an string specifying the name of the entities to fetch
- cellTextLabelKey - they key to use for the textLabel.text property of the
UITableViewCell
s
The following properties can optionally be set to add functionality to
the NLFetchedResultsController
:
- viewTitle - use this to specify the title for the view
- reusableCellIdentifier - the reusableCellIdentifier used by the
UITableViewCell
s that are created - sectionNameKeyPath - use this to specify the key path to use for sections in the table view
- sortDescriptors - an
NSArray
of sortDescriptors to use. By default anNLFetchedResultsTable
will sort by the cellTextLabelKey. - resultsCacheName - use this to manually specify the name for the frc's results cache
- cellDetailTextLabelKey - an optional key to use for the cells' detailTextLabel.text
This code is free for anyone to use in personal or commercial projects under the Creative Commons Attribution 3.0 license (http://creativecommons.org/licenses/by/3.0/).
Any comments or suggestions that would help improve this code are always welcome! Enjoy!