Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielgarbien/b2c38b9f8a2517bf7d5916732eb6a0c3 to your computer and use it in GitHub Desktop.
Save danielgarbien/b2c38b9f8a2517bf7d5916732eb6a0c3 to your computer and use it in GitHub Desktop.
import Foundation
import UIKit
import CoreData
class FetchedResultsTableDataSource: NSObject {
typealias CellForRowAtIndexPath = (tableView: UITableView, indexPath: NSIndexPath, object: AnyObject) -> UITableViewCell
let fetchedResultController: NSFetchedResultsController
private let cellForRowAtIndexPath: CellForRowAtIndexPath
init(fetchedResultController: NSFetchedResultsController, cellForRowAtIndexPath: CellForRowAtIndexPath) {
self.fetchedResultController = fetchedResultController
self.cellForRowAtIndexPath = cellForRowAtIndexPath
}
}
extension FetchedResultsTableDataSource: UITableViewDataSource {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return fetchedResultController.sections?.count ?? 0
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fetchedResultController.sections?[section].numberOfObjects ?? 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let object = fetchedResultController.objectAtIndexPath(indexPath)
return cellForRowAtIndexPath(tableView: tableView, indexPath: indexPath, object: object)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment