Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Simple MVVM ViewController
- class PlansViewController: UIViewController {
- typealias ArrangedType = Plan
- var viewModel: PlansViewModel
- var delegate: PlansViewControllerDelegate?
- @IBOutlet var tableView: UITableView!
- required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }
- init(viewModel: PlansViewModel) {
- self.viewModel = viewModel
- super.init(nibName: nil, bundle: nil)
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- tableView.delegate = self
- tableView.register(R.nib.planSummaryCell(), forCellReuseIdentifier: R.nib.planSummaryCell.identifier)
- viewModel.tableDataSource = ArrangedTableViewDataSource(tableView: tableView, arrangedObjects: viewModel.arrangedObjects, cellBuilder: { (tableView, indexPath, object) -> (UITableViewCell) in
- let cell = tableView.dequeueReusableCell(withIdentifier: R.nib.planSummaryCell.identifier) as! PlanSummaryCell
- cell.textLabel?.text = (object as? BudgetPlan)?.title ?? object.id
- return cell
- })
- }
- }
- extension PlansViewController: UITableViewDelegate {
- // Only Triggered by a long tap ???
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- self.delegate?.plansViewController(self, didSelectPlan: viewModel.arrangedObjects[indexPath])
- }
- }
- // 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
- class ArrangedTableViewDataSource<ArrangedType: Equatable>: NSObject, UITableViewDataSource {
- fileprivate let cellBuilder: (UITableView, IndexPath, ArrangedType) -> (UITableViewCell)
- fileprivate let arrangedObjects: ArrangedObjects<ArrangedType>
- fileprivate let tableView: UITableView
- init(tableView: UITableView, arrangedObjects: ArrangedObjects<ArrangedType>, cellBuilder: @escaping (UITableView, IndexPath, ArrangedType) -> (UITableViewCell)) {
- self.tableView = tableView
- self.arrangedObjects = arrangedObjects
- self.cellBuilder = cellBuilder
- super.init()
- self.tableView.dataSource = self
- self.arrangedObjects.delegate = self
- }
- func numberOfSections(in tableView: UITableView) -> Int {
- return arrangedObjects.arranged.count
- }
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return arrangedObjects.arranged[section].count
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- return cellBuilder(tableView, indexPath, arrangedObjects[indexPath])
- }
- }
- extension ArrangedTableViewDataSource: ArrangedObjectsDelegate {
- func arrangedObjects<ArrangedType>(_ arrangedObjects: ArrangedObjects<ArrangedType>, didInsertSection section: Int) {
- tableView.insertSections([section], with:.automatic)
- }
- func arrangedObjects<ArrangedType>(_ arrangedObjects: ArrangedObjects<ArrangedType>, didRemoveSection section: Int) {
- tableView.deleteSections([section], with: .automatic)
- }
- func arrangedObjects<ArrangedType>(_ arrangedObjects: ArrangedObjects<ArrangedType>, willInsertObject: ArrangedType) {
- self.tableView.beginUpdates()
- }
- func arrangedObjects<ArrangedType>(_ arrangedObjects: ArrangedObjects<ArrangedType>, willRemoveObject: ArrangedType) {
- self.tableView.beginUpdates()
- }
- func arrangedObjects<ArrangedType>(_ arrangedObjects: ArrangedObjects<ArrangedType>, didInsertObject: ArrangedType) {
- self.tableView.insertRows(at: [arrangedObjects.indexPath(for: didInsertObject)!], with: .automatic)
- self.tableView.endUpdates()
- }
- func arrangedObjects<ArrangedType>(_ arrangedObjects: ArrangedObjects<ArrangedType>, didRemoveObject: ArrangedType) {
- self.tableView.deleteRows(at: arrangedObjects.popRemovableIndexPaths(), with: .automatic)
- self.tableView.endUpdates()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement