Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save oliverbarreto/4162b41aa050d630d735610854525fd9 to your computer and use it in GitHub Desktop.
Save oliverbarreto/4162b41aa050d630d735610854525fd9 to your computer and use it in GitHub Desktop.
FetchedResultsTableViewController
//
// FetchedResultsTableViewController.swift
// SmashtagA5
//
// Created by Paul Hegarty on 2/3/17.
// Copyright © 2017 Stanford University. All rights reserved.
//
import UIKit
import CoreData
class FetchedResultsTableViewController: UITableViewController, NSFetchedResultsControllerDelegate
{
public func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.beginUpdates()
}
public func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
switch type {
case .insert: tableView.insertSections([sectionIndex], with: .fade)
case .delete: tableView.deleteSections([sectionIndex], with: .fade)
default: break
}
}
public func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .insert:
tableView.insertRows(at: [newIndexPath!], with: .fade)
case .delete:
tableView.deleteRows(at: [indexPath!], with: .fade)
case .update:
tableView.reloadRows(at: [indexPath!], with: .fade)
case .move:
tableView.deleteRows(at: [indexPath!], with: .fade)
tableView.insertRows(at: [newIndexPath!], with: .fade)
}
}
public func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.endUpdates()
}
}
extension ***YOUR_CLASS_HERE***
{
// MARK: UITableViewDataSource Common Methods
override func numberOfSections(in tableView: UITableView) -> Int {
return fetchedResultsController?.sections?.count ?? 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let sections = fetchedResultsController?.sections, sections.count > 0 {
return sections[section].numberOfObjects
} else {
return 0
}
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if let sections = fetchedResultsController?.sections, sections.count > 0 {
return sections[section].name
} else {
return nil
}
}
override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return fetchedResultsController?.sectionIndexTitles
}
override func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
return fetchedResultsController?.section(forSectionIndexTitle: title, at: index) ?? 0
}
// MARK: UITableViewDataSource - Configure Cell here with objetct at indexPath
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Your_Cell_ID_GOES_HERE", for: indexPath)
if let twitterUser = fetchedResultsController?.object(at: indexPath) {
// Configure Cell here
}
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment