Created
May 2, 2017 13:42
-
-
Save vamsiac/3a68f17fcc8b659b874eb57abaea871d 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
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