Database Reference
In-Depth Information
let sectionInfo = fetchedResultsController . sections ! [section]
as NSFetchedResultsSectionInfo
return sectionInfo. numberOfObjects
}
The number of sections in the table view corresponds to the number of sections in
the fetched results controller. You may be wondering how this table view can have
more than one section. Aren't you simply fetching and displaying all Teams ?
That's correct. You will only have one section this time around, but keep it in the
back of your mind that NSFetchedResultsController can split up your data into
sections. You'll see an example of this later in the chapter.
Furthermore, the number of rows in each table view section corresponds to the
number of objects in each fetched results controller section. You can query
information about a fetched results controller section through its sections property.
Note: The sections array contains opaque objects that implement the
NSFetchedResultsSectionInfo protocol. This lightweight protocol provides
information about a section, such as its title and number of objects.
Implementing tableView(_:cellForRowAtIndexPath:) would typically be the next
step. A quick look at the method, however, reveals that it is already vending
TeamCell cells as needed. What you need to change is the helper method that
populates the cell.
Below cellForRowAtIndexPath , re-implement configureCell :
func configureCell(cell: TeamCell , indexPath: NSIndexPath ) {
let team =
fetchedResultsController . objectAtIndexPath (indexPath) as Team
cell. flagImageView . image = UIImage (named: team. imageName )
cell. teamLabel . text = team. teamName
cell. scoreLabel . text = "Wins: \(team. wins ) "
}
This method takes in a TeamCell object and an index path. You use this index path
to grab the corresponding Team object from the fetched results controller. Then you
use this Core Data object to populate the cell's flag image, team name and score
label.
Notice again that there's no array variable holding your teams. They're all stored
inside the fetched results controller and you access them via objectAtIndexPath .
It's time to test your creation. Build and run the app. Ready, set and... crash?
Search WWH ::




Custom Search