Database Reference
In-Depth Information
cacheName: nil )
The difference here is you're passing in a value for the optional sectionNameKeyPath
parameter. You can use this parameter to specify an attribute that the fetched
results controller should use to group the results and generate sections.
How exactly are these sections generated? Each unique attribute value becomes a
section. NSFetchedResultsController then groups its fetched results into these
sections. In this case, it will generate sections for each unique value of
qualifyingZone such as “Africa”, “Asia”, “Oceania” and so on. This is exactly what
you want!
Note: sectionNameKeyPath takes a keyPath string. It can take the form of an
attribute name such as “ qualifyingZone ” or “ teamName ”, or it can drill deep into
a Core Data relationship, such as “ employee.address.street ”.
The fetched results controller will now report the sections and rows to the table
view, but the current UI won't look any different. To fix this problem, add the
following method to the class:
func tableView(tableView: UITableView ,
titleForHeaderInSection section: Int ) -> String ? {
let sectionInfo = fetchedResultsController . sections! [section]
as NSFetchedResultsSectionInfo
return sectionInfo. name
}
Implementing this data source method adds section headers to the table view,
making it easy to see where one section ends and another one begins. In this case,
the section gets its title from the qualifying zone. Like before, this information
comes directly from the NSFetchedResultsSectionInfo protocol.
Build and run the application. Your app will look something like the following:
Search WWH ::




Custom Search