Created
September 12, 2014 19:41
-
-
Save kristopherjohnson/d5dd8c6babe56f567184 to your computer and use it in GitHub Desktop.
Swift boilerplate NSFetchedResultsControllerDelegate methods for a UITableViewController subclass
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
http://oleb.net/blog/2013/02/nsfetchedresultscontroller-documentation-bug/