Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. import UIKit
  2. import CoreData
  3.  
  4. public protocol FetchedResultsDelegate {
  5. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: AnyObject) -> UITableViewCell
  6. }
  7.  
  8. public class FetchedResultsDataSource: NSObject, UITableViewDataSource, NSFetchedResultsControllerDelegate {
  9.  
  10. public weak var tableView: UITableView?
  11. public var frc: NSFetchedResultsController
  12. public var delegate: FetchedResultsDelegate?
  13.  
  14. public init(fetchRequest: NSFetchRequest, context: NSManagedObjectContext = DataController.dataStack.mainContext, sectionNameKeyPath: String?) {
  15. frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: sectionNameKeyPath, cacheName: nil)
  16.  
  17. super.init()
  18.  
  19. frc.delegate = self
  20. }
  21.  
  22. /// Set `tableView.datasource` to `self`, perform fetch and reload table data
  23. public func performFetchAndConfigureTableView(tableView: UITableView) {
  24. self.tableView = tableView
  25.  
  26. tableView.dataSource = self
  27.  
  28. do {
  29. try frc.performFetch()
  30. dispatch_async(dispatch_get_main_queue()) { tableView.reloadData() }
  31. } catch {
  32. assertionFailure("\(#function): \(error)")
  33. }
  34. }
  35.  
  36. public func objectAtIndexPath(indexPath: NSIndexPath) -> AnyObject {
  37. return frc.objectAtIndexPath(indexPath)
  38. }
  39.  
  40. public func titleForHeaderInSection(section: Int) -> String? {
  41. return self.frc.sections?[section].name
  42. }
  43.  
  44. // MARK: - UITableViewDataSource
  45.  
  46. public func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  47. return self.frc.sections?.count ?? 0
  48. }
  49.  
  50. public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  51. guard let sections = frc.sections else { return 0 }
  52.  
  53. return sections[section].numberOfObjects
  54. }
  55.  
  56. public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  57. if let delegate = delegate {
  58. return delegate.tableView(tableView, cellForRowAtIndexPath: indexPath, object: self.objectAtIndexPath(indexPath))
  59. } else {
  60. assertionFailure("Delegate is not set, can't return cell")
  61. return UITableViewCell()
  62. }
  63. }
  64.  
  65. // func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  66. // return self.frc.sections?[section].name
  67. // }
  68.  
  69.  
  70.  
  71. // MARK: - Right section titles
  72.  
  73. // func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
  74. // return self.frc.sectionForSectionIndexTitle(title, atIndex: index) ?? 0
  75. // }
  76. //
  77. // func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
  78. // return self.frc.sectionIndexTitles
  79. // }
  80.  
  81. // MARK: - NSFetchedResultsControllerDelegate
  82.  
  83. public func controllerWillChangeContent(controller: NSFetchedResultsController) {
  84. tableView?.beginUpdates()
  85. }
  86.  
  87. public func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
  88.  
  89. let sectionIndexSet = NSIndexSet(index: sectionIndex)
  90.  
  91. switch type {
  92. case NSFetchedResultsChangeType.Insert:
  93. tableView?.insertSections(sectionIndexSet, withRowAnimation: .Automatic)
  94. case NSFetchedResultsChangeType.Delete:
  95. tableView?.deleteSections(sectionIndexSet, withRowAnimation: .Automatic)
  96. case .Update:
  97. tableView?.reloadSections(sectionIndexSet, withRowAnimation: .Automatic)
  98. case .Move:
  99. // TODO: maybe we have better solution instead of reload, possible bugs
  100. tableView?.reloadSections(sectionIndexSet, withRowAnimation: .Automatic)
  101. }
  102. }
  103.  
  104. public func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
  105. switch type {
  106. case .Insert:
  107. guard let newIndexPath = newIndexPath else { return }
  108. tableView?.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Automatic)
  109. case .Delete:
  110. guard let indexPath = indexPath else { return }
  111. tableView?.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
  112. case .Update:
  113. guard let indexPath = indexPath else { return }
  114. tableView?.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
  115. case .Move:
  116. guard let indexPath = indexPath, let newIndexPath = newIndexPath else { return }
  117. tableView?.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
  118. tableView?.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)
  119. }
  120. }
  121.  
  122. public func controllerDidChangeContent(controller: NSFetchedResultsController) {
  123. tableView?.endUpdates()
  124. }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement