Advertisement
Guest User

Untitled

a guest
Jan 11th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.15 KB | None | 0 0
  1. //  Simple MVVM ViewController
  2. class PlansViewController: UIViewController {
  3.     typealias ArrangedType = Plan
  4.     var viewModel: PlansViewModel
  5.    
  6.     var delegate: PlansViewControllerDelegate?
  7.        
  8.     @IBOutlet var tableView: UITableView!
  9.    
  10.     required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }
  11.     init(viewModel: PlansViewModel) {
  12.         self.viewModel = viewModel
  13.         super.init(nibName: nil, bundle: nil)
  14.     }
  15.    
  16.     override func viewDidLoad() {
  17.         super.viewDidLoad()
  18.         tableView.delegate = self
  19.         tableView.register(R.nib.planSummaryCell(), forCellReuseIdentifier: R.nib.planSummaryCell.identifier)
  20.        
  21.         viewModel.tableDataSource = ArrangedTableViewDataSource(tableView: tableView, arrangedObjects: viewModel.arrangedObjects, cellBuilder: { (tableView, indexPath, object) -> (UITableViewCell) in
  22.             let cell = tableView.dequeueReusableCell(withIdentifier: R.nib.planSummaryCell.identifier) as! PlanSummaryCell
  23.             cell.textLabel?.text = (object as? BudgetPlan)?.title ?? object.id
  24.             return cell
  25.         })
  26.     }
  27. }
  28.  
  29. extension PlansViewController: UITableViewDelegate {
  30.     //  Only Triggered by a long tap ???
  31.     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  32.         self.delegate?.plansViewController(self, didSelectPlan: viewModel.arrangedObjects[indexPath])
  33.     }
  34. }
  35.  
  36. //  DataSource: Simple object, holding the data in a "ArrangedObjects"-class, which is at least a generic nested array  which filters and groups its contents and notifies it delegate about changes  
  37. class ArrangedTableViewDataSource<ArrangedType: Equatable>: NSObject, UITableViewDataSource {
  38.     fileprivate let cellBuilder: (UITableView, IndexPath, ArrangedType) -> (UITableViewCell)
  39.     fileprivate let arrangedObjects: ArrangedObjects<ArrangedType>
  40.     fileprivate let tableView: UITableView
  41.    
  42.     init(tableView: UITableView, arrangedObjects: ArrangedObjects<ArrangedType>, cellBuilder: @escaping (UITableView, IndexPath, ArrangedType) -> (UITableViewCell)) {
  43.         self.tableView = tableView
  44.         self.arrangedObjects = arrangedObjects
  45.         self.cellBuilder = cellBuilder
  46.        
  47.         super.init()
  48.        
  49.         self.tableView.dataSource = self
  50.         self.arrangedObjects.delegate = self
  51.     }
  52.    
  53.     func numberOfSections(in tableView: UITableView) -> Int {
  54.         return arrangedObjects.arranged.count
  55.     }
  56.    
  57.     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  58.         return arrangedObjects.arranged[section].count
  59.     }
  60.    
  61.     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  62.         return cellBuilder(tableView, indexPath, arrangedObjects[indexPath])
  63.     }
  64. }
  65.  
  66. extension ArrangedTableViewDataSource: ArrangedObjectsDelegate {
  67.     func arrangedObjects<ArrangedType>(_ arrangedObjects: ArrangedObjects<ArrangedType>, didInsertSection section: Int) {
  68.         tableView.insertSections([section], with:.automatic)
  69.     }
  70.    
  71.     func arrangedObjects<ArrangedType>(_ arrangedObjects: ArrangedObjects<ArrangedType>, didRemoveSection section: Int) {
  72.         tableView.deleteSections([section], with: .automatic)
  73.     }
  74.    
  75.     func arrangedObjects<ArrangedType>(_ arrangedObjects: ArrangedObjects<ArrangedType>, willInsertObject: ArrangedType) {
  76.         self.tableView.beginUpdates()
  77.     }
  78.    
  79.     func arrangedObjects<ArrangedType>(_ arrangedObjects: ArrangedObjects<ArrangedType>, willRemoveObject: ArrangedType) {
  80.         self.tableView.beginUpdates()
  81.     }
  82.    
  83.    
  84.     func arrangedObjects<ArrangedType>(_ arrangedObjects: ArrangedObjects<ArrangedType>, didInsertObject: ArrangedType) {
  85.         self.tableView.insertRows(at: [arrangedObjects.indexPath(for: didInsertObject)!], with: .automatic)
  86.         self.tableView.endUpdates()
  87.     }
  88.    
  89.     func arrangedObjects<ArrangedType>(_ arrangedObjects: ArrangedObjects<ArrangedType>, didRemoveObject: ArrangedType) {
  90.         self.tableView.deleteRows(at: arrangedObjects.popRemovableIndexPaths(), with: .automatic)
  91.         self.tableView.endUpdates()
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement