Advertisement
Guest User

TableViewManager

a guest
Apr 4th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.70 KB | None | 0 0
  1. import CoreData
  2. import UIKit
  3.  
  4. protocol TableViewManagerDelegate: AnyObject {
  5.    
  6.     associatedtype Cell: UITableViewCell
  7.     associatedtype Header: UITableViewHeaderFooterView
  8.     associatedtype Footer: UITableViewHeaderFooterView
  9.     associatedtype Object: NSManagedObject
  10.    
  11.     func configureCell(_ cell: Cell, for object: Object)
  12.     func configureHeader(_ header: Header, for title: String?)
  13.     func configureFooter(_ footer: Footer, for title: String?)
  14.    
  15. }
  16.  
  17. // MARK: -
  18.  
  19. extension TableViewManagerDelegate {
  20.    
  21.     func configureHeader(
  22.         _ header: UITableViewHeaderFooterView,
  23.         for title: String?
  24.     ) { }
  25.    
  26.     func configureFooter(
  27.         _ footer: UITableViewHeaderFooterView,
  28.         for title: String?
  29.     ) { }
  30.    
  31. }
  32.  
  33. // MARK: -
  34.  
  35. class TableViewManager<Delegate: TableViewManagerDelegate>:
  36.     NSObject,
  37.     UITableViewDataSource,
  38.     UITableViewDelegate,
  39.     NSFetchedResultsControllerDelegate
  40. {
  41.    
  42.     // MARK: - Types
  43.    
  44.     typealias Cell = Delegate.Cell
  45.     typealias Header = Delegate.Header
  46.     typealias Footer = Delegate.Footer
  47.     typealias Object = Delegate.Object
  48.    
  49.     // MARK: - Variables
  50.    
  51.     weak var delegate: Delegate?
  52.    
  53.     private let tableView: UITableView
  54.     private let frc: NSFetchedResultsController<Object>
  55.    
  56.     // MARK: - Lifecycle
  57.    
  58.     init(
  59.         tableView: UITableView,
  60.         frc: NSFetchedResultsController<Object>
  61.     ) {
  62.         self.tableView = tableView
  63.         self.frc = frc
  64.        
  65.         super.init()
  66.     }
  67.    
  68.     // MARK: - Public
  69.    
  70.     func object(at indexPath: IndexPath) -> Object {
  71.         return frc.object(at: indexPath)
  72.     }
  73.    
  74.     func title(at section: Int) -> String? {
  75.         return frc.sections?[safe: section]?.name ?? nil
  76.     }
  77.    
  78.     // MARK: - UITableViewDataSource
  79.    
  80.     func numberOfSections(
  81.         in tableView: UITableView
  82.     ) -> Int {
  83.         return frc.sections?.count ?? 0
  84.     }
  85.    
  86.     func tableView(
  87.         _ tableView: UITableView,
  88.         numberOfRowsInSection section: Int
  89.     ) -> Int {
  90.         return frc.sections?[section].numberOfObjects ?? 0
  91.     }
  92.    
  93.     func tableView(
  94.         _ tableView: UITableView,
  95.         cellForRowAt indexPath: IndexPath
  96.     ) -> UITableViewCell {
  97.         guard let cell = tableView.dequeueReusableCell(
  98.             withIdentifier: Cell.name,
  99.             for: indexPath
  100.         ) as? Cell else {
  101.             fatalError("Failed to get \(Cell.name)")
  102.         }
  103.         delegate?.configureCell(cell, for: object(at: indexPath))
  104.         return cell
  105.     }
  106.    
  107.     // MARK: - UITableViewDelegate
  108.    
  109.     func tableView(
  110.         _ tableView: UITableView,
  111.         viewForHeaderInSection section: Int
  112.     ) -> UIView? {
  113.         guard let header = tableView.dequeueReusableHeaderFooterView(
  114.             withIdentifier: Header.name
  115.         ) as? Header else {
  116.             fatalError("Failed to get \(Header.name)")
  117.         }
  118.         delegate?.configureHeader(header, for: title(at: section))
  119.         return header
  120.     }
  121.    
  122.     func tableView(
  123.         _ tableView: UITableView,
  124.         viewForFooterInSection section: Int
  125.     ) -> UIView? {
  126.         guard let footer = tableView.dequeueReusableHeaderFooterView(
  127.             withIdentifier: Footer.name
  128.         ) as? Footer else {
  129.             fatalError("Failed to get \(Footer.name)")
  130.         }
  131.         delegate?.configureFooter(footer, for: title(at: section))
  132.         return footer
  133.     }
  134.    
  135. }
  136.  
  137. // MARK: -
  138.  
  139. class TestViewController: UIViewController, TableViewManagerDelegate {
  140.    
  141.     func configureCell(
  142.         _ cell: UITableViewCell,
  143.         for object: NSManagedObject
  144.     ) {
  145.        
  146.     }
  147.    
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement