Guest User

Untitled

a guest
Sep 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. //
  2. // BusListViewController.swift
  3. // QLead
  4. //
  5. // Created by Raed Fayad on 2018-08-26.
  6. // Copyright © 2018 kirkbyo. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import Foundation
  11.  
  12. struct Model {
  13. var title: String
  14. var bus: String
  15. }
  16.  
  17. let dataArray = [
  18. [Model(title: "Name1", bus: "Bus 1"), Model(title: "Name2", bus: "Bus 1"), Model(title: "Name3", bus: "Bus 1"),Model(title: "Name4", bus: "Bus 1"), Model(title: "Name5", bus: "Bus 1"), Model(title: "Name6", bus: "Bus 1"), Model(title: "Name7", bus: "Bus 1"), Model(title: "Name8", bus: "Bus 1")] ,
  19. [Model(title: "Name9", bus: "Bus 2"), Model(title: "Name10", bus: "Bus 2"), Model(title: "Name11", bus: "Bus 2"), Model(title: "Name12", bus: "Bus 2"), Model(title: "Name13", bus: "Bus 2")],
  20. [Model(title: "Name14", bus: "Bus 3"), Model(title: "Name15", bus: "Bus 3"), Model(title: "Name16", bus: "Bus 3")]]
  21.  
  22. class ViewModelItem {
  23. private var item: Model
  24. var isSelected = false
  25. var title: String {
  26. return item.title
  27. }
  28. init(item: Model) {
  29. self.item = item
  30. }
  31. }
  32.  
  33. class ViewModel: NSObject {
  34. var items = [ViewModelItem]()
  35.  
  36. var didToggleSelection: ((_ hasSelection: Bool) -> ())? {
  37. didSet {
  38. didToggleSelection?(!selectedItems.isEmpty)
  39. }
  40. }
  41.  
  42. var selectedItems: [ViewModelItem] {
  43. return items.filter { return $0.isSelected }
  44. }
  45.  
  46. override init() {
  47. super.init()
  48. let temp = dataArray.flatMap { $0 }
  49. items = temp.map { ViewModelItem(item: $0) }
  50. }
  51. }
  52.  
  53. extension ViewModel: UITableViewDataSource {
  54.  
  55. func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  56. return dataArray.count
  57. }
  58. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  59. return dataArray[section].count
  60. }
  61. func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  62. return dataArray[section][0].bus
  63.  
  64. }
  65. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  66.  
  67. if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? BusListTableViewCell {
  68.  
  69. cell.titleLabel.text = dataArray[indexPath.section][indexPath.row].title
  70.  
  71. // select/deselect the cell
  72. if items[indexPath.row].isSelected {
  73. if !cell.isSelected {
  74. tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
  75. }
  76. } else {
  77. if cell.isSelected {
  78. tableView.deselectRow(at: indexPath, animated: false)
  79. }
  80. }
  81.  
  82. return cell
  83. }
  84. return UITableViewCell()
  85. }
  86. }
  87.  
  88. extension ViewModel: UITableViewDelegate {
  89. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  90.  
  91. // update ViewModel item
  92. items[indexPath.row].isSelected = true
  93.  
  94. didToggleSelection?(!selectedItems.isEmpty)
  95. }
  96.  
  97. func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
  98.  
  99. // update ViewModel item
  100. items[indexPath.row].isSelected = false
  101.  
  102. didToggleSelection?(!selectedItems.isEmpty)
  103. }
  104.  
  105. func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath?{
  106. return indexPath
  107. }
  108. }
  109.  
  110.  
  111. class BusListViewController: UIViewController {
  112.  
  113. var viewModel = ViewModel()
  114.  
  115. @IBOutlet weak var tableView: UITableView!
  116. @IBOutlet weak var nextButton: UIButton!
  117. @IBAction func buttonPressed(_ sender: Any) {
  118.  
  119. }
  120.  
  121. override func viewDidLoad() {
  122. super.viewDidLoad()
  123.  
  124. tableView?.estimatedRowHeight = 100
  125. tableView?.rowHeight = UITableViewAutomaticDimension
  126. tableView?.allowsMultipleSelection = true
  127. tableView?.dataSource = viewModel
  128. tableView?.delegate = viewModel
  129. tableView?.separatorStyle = .none
  130.  
  131. viewModel.didToggleSelection = { [weak self] hasSelection in
  132. self?.nextButton.isEnabled = hasSelection
  133. }
  134. }
  135.  
  136. override func didReceiveMemoryWarning() {
  137. super.didReceiveMemoryWarning()
  138. }
Add Comment
Please, Sign In to add comment