Skip to content

Instantly share code, notes, and snippets.

@vamsiac
Created May 2, 2017 13:42
Show Gist options
  • Save vamsiac/3a68f17fcc8b659b874eb57abaea871d to your computer and use it in GitHub Desktop.
Save vamsiac/3a68f17fcc8b659b874eb57abaea871d to your computer and use it in GitHub Desktop.
import UIKit
import CoreData
class FRCTableViewDataSource: NSObject, UITableViewDataSource {
private unowned let fetchedResultsController: NSFetchedResultsController<NSFetchRequestResult>
let configureCell: (UITableViewCell, IndexPath)-> ()
init(fetchController: NSFetchedResultsController<NSFetchRequestResult>, configureBlock: @escaping (UITableViewCell, IndexPath)-> ()) {
self.fetchedResultsController = fetchController
self.configureCell = configureBlock
super.init()
}
func numberOfSections(in tableView: UITableView) -> Int {
return self.fetchedResultsController.sections?.count ?? 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sectionInfo = self.fetchedResultsController.sections![section]
return sectionInfo.numberOfObjects
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
self.configureCell(cell!, indexPath)
return cell!
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let object = self.fetchedResultsController.object(at: indexPath) as! Deletable
let confirmAlert = UIAlertController(alertType: AlertType.confirmationAlert(confirmationBlock: {
object.delete({ result in
switch result {
case .success:
print("\(type(of: object)) deleted")
tableView.reloadSectionIndexTitles()
default: break
}
})
}))
confirmAlert.show()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment