Forked from kristopherjohnson/NSFetchedResultsControllerDelegate_Boilerplate.swift
Last active
August 29, 2015 14:20
-
-
Save GJNilsen/7ded113ff6db9d6a73d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: NSFetchedResultsControllerDelegate | |
func controllerWillChangeContent(controller: NSFetchedResultsController) { | |
self.tableView.beginUpdates() | |
} | |
func controller(controller: NSFetchedResultsController, | |
didChangeObject anObject: AnyObject, | |
atIndexPath indexPath: NSIndexPath?, | |
forChangeType type: NSFetchedResultsChangeType, | |
newIndexPath: NSIndexPath?) | |
{ | |
switch(type) { | |
case .Insert: | |
if let newIndexPath = newIndexPath { | |
tableView.insertRowsAtIndexPaths([newIndexPath], | |
withRowAnimation:UITableViewRowAnimation.Fade) | |
} | |
case .Delete: | |
if let indexPath = indexPath { | |
tableView.deleteRowsAtIndexPaths([indexPath], | |
withRowAnimation: UITableViewRowAnimation.Fade) | |
} | |
case .Update: | |
if let indexPath = indexPath { | |
if let cell = tableView.cellForRowAtIndexPath(indexPath) as? UITableViewCell { | |
configureCell(cell, withObject: object) | |
} | |
} | |
case .Move: | |
if let indexPath = indexPath { | |
if let newIndexPath = newIndexPath { | |
tableView.deleteRowsAtIndexPaths([indexPath], | |
withRowAnimation: UITableViewRowAnimation.Fade) | |
tableView.insertRowsAtIndexPaths([newIndexPath], | |
withRowAnimation: UITableViewRowAnimation.Fade) | |
} | |
} | |
} | |
} | |
func controller(controller: NSFetchedResultsController, | |
didChangeSection sectionInfo: NSFetchedResultsSectionInfo, | |
atIndex sectionIndex: Int, | |
forChangeType type: NSFetchedResultsChangeType) | |
{ | |
switch(type) { | |
case .Insert: | |
tableView.insertSections(NSIndexSet(index: sectionIndex), | |
withRowAnimation: UITableViewRowAnimation.Fade) | |
case .Delete: | |
tableView.deleteSections(NSIndexSet(index: sectionIndex), | |
withRowAnimation: UITableViewRowAnimation.Fade) | |
default: | |
break | |
} | |
} | |
func controllerDidChangeContent(controller: NSFetchedResultsController) { | |
tableView.endUpdates() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment