CoreDataTableViewController

public class CoreDataTableViewController: UITableViewController, NSFetchedResultsControllerDelegate

Swift version of class originaly created for Stanford CS193p Winter 2013.

This class mostly just copies the code from NSFetchedResultsController documentation page into a subclass of UITableViewController.

Just subclass this and set the fetchedResultsController property. The only UITableViewDataSource method you’ll HAVE to implement is tableView:cellForRowAtIndexPath:. And you can use the NSFetchedResultsController method objectAtIndexPath: to do it.

Remember that once you create an NSFetchedResultsController, you CANNOT modify its properties. If you want new fetch parameters (predicate, sorting, etc.), create a NEW NSFetchedResultsController and set this class’s fetchedResultsController property again.

  • The controller (this class fetches nothing if this is not set).

    Declaration

    Swift

    public var fetchedResultsController: NSFetchedResultsController?
  • Causes the fetchedResultsController to refetch the data. You almost certainly never need to call this. The NSFetchedResultsController class observes the context (so if the objects in the context change, you do not need to call performFetch since the NSFetchedResultsController will notice and update the table automatically). This will also automatically be called if you change the fetchedResultsController property.

    Declaration

    Swift

    public func performFetch() throws
  • Turn this on before making any changes in the managed object context that are a one-for-one result of the user manipulating rows directly in the table view. Such changes cause the context to report them (after a brief delay), and normally our fetchedResultsController would then try to update the table, but that is unnecessary because the changes were made in the table already (by the user) so the fetchedResultsController has nothing to do and needs to ignore those reports. Turn this back off after the user has finished the change. Note that the effect of setting this to NO actually gets delayed slightly so as to ignore previously-posted, but not-yet-processed context-changed notifications, therefore it is fine to set this to YES at the beginning of, e.g., tableView:moveRowAtIndexPath:toIndexPath:, and then set it back to NO at the end of your implementation of that method. It is not necessary (in fact, not desirable) to set this during row deletion or insertion (but definitely for row moves).

    Declaration

    Swift

    public var suspendAutomaticTrackingOfChangesInManagedObjectContext: Bool
  • Notifies the receiver that the fetched results controller is about to start processing of one or more changes due to an add, remove, move, or update.

    :param: controller The fetched results controller that sent the message.

    Declaration

    Swift

    public func controllerWillChangeContent(controller: NSFetchedResultsController)
  • Notifies the receiver of the addition or removal of a section.

    :param: controller The fetched results controller that sent the message. :param: sectionInfo The section that changed. :param: sectionIndex The index of the changed section. :param: type The type of change (insert or delete).

    Declaration

    Swift

    public func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType)
  • Notifies the receiver that a fetched object has been changed due to an add, remove, move, or update.

    :param: controller The fetched results controller that sent the message. :param: anObject The object in controller’s fetched results that changed. :param: indexPath The index path of the changed object (this value is nil for insertions). :param: type The type of change. :param: newIndexPath The destination path for the object for insertions or moves (this value is nil for a deletion).

    Declaration

    Swift

    public func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?)
  • Notifies the receiver that the fetched results controller has completed processing of one or more changes due to an add, remove, move, or update.

    :param: controller The fetched results controller that sent the message.

    Declaration

    Swift

    public func controllerDidChangeContent(controller: NSFetchedResultsController)
  • Asks the data source to return the number of sections in the table view.

    :param: tableView An object representing the table view requesting this information.

    :returns: The number of sections in tableView.

    Declaration

    Swift

    public override func numberOfSectionsInTableView(tableView: UITableView) -> Int
  • Tells the data source to return the number of rows in a given section of a table view. (required)

    :param: tableView The table-view object requesting this information. :param: section An index number identifying a section in tableView.

    :returns: The number of rows in section.

    Declaration

    Swift

    public override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
  • Asks the data source for the title of the header of the specified section of the table view.

    :param: tableView An object representing the table view requesting this information. :param: section An index number identifying a section in tableView.

    :returns: A string to use as the title of the section header.

    Declaration

    Swift

    public override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
  • Asks the data source to return the index of the section having the given title and section title index.

    :param: tableView An object representing the table view requesting this information. :param: title The title as displayed in the section index of tableView. :param: index An index number identifying a section title in the array returned by sectionIndexTitlesForTableView:.

    :returns: An index number identifying a section.

    Declaration

    Swift

    public override func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int
  • Asks the data source to return the titles for the sections for a table view.

    :param: tableView An object representing the table view requesting this information.

    :returns: An array of strings that serve as the title of sections in the table view and appear in the index list on the right side of the table view.

    Declaration

    Swift

    public override func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]?