Index path access—
The fetched results class offers object-index path integration in two
directions. You can recover objects from a fetched object array using
index paths by calling objectAtIndexPath:. You can query for the index path associated with a fetched object by calling indexPathForObject:.
These two methods work with both sectioned tables and those tables that
are flat—that is, that only use a single section for all their data.
Section key path— The sectionNameKeyPath
property links a managed object attribute to section names. This
property helps determine which section each managed object belongs to.
You can set this property directly at any time or you initialize it when
you set up your fetched results controller.
Recipe 19-1 uses an attribute named section
to distinguish sections, although you can use any attribute name for
this key path. For this example, this attribute uses the first character
of each object name to assign a managed object to a section. Set the
key path to nil to produce a flat table without sections.
Section groups— Recover section subgroups with the sections
property. This property returns an array of sections, each of which
stores the managed objects whose section attribute maps to the same
letter.
Each returned section implements the NSFetchedResultsSectionInfo protocol. This protocol ensures that sections can report their objects and numberOfObjects, their name, and an indexTitle, that is, the title that appears on the quick reference index optionally shown above and at the right of the table.
Index titles— The sectionIndexTitles property generates a list of section titles from the sections within the fetched data. For Recipe 19-1,
that array includes single letter titles. The default implementation
uses the value of each section key to return a list of all known
sections.
Two further instance methods, sectionIndexTitleForSectionName: and sectionForSectionIndexTitle:atIndex:,
provide section title lookup features. The first returns a title for a
section name. The second looks up a section via its title. Override
these to use section titles that do not match the data stored in the
section name key.
self.fetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:self.context
sectionNameKeyPath:@"section" cacheName:@"Root"];