BaseTableViewController.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // BaseTableViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by zhongyuan on 2018/4/11.
  6. // Copyright © 2018年 zhongyuan.All rights reserved.
  7. //
  8. import UIKit
  9. class BaseTableViewController<Model>: BaseViewController, UITableViewDataSource, UITableViewDelegate {
  10. /// 要显示的数据
  11. var tableCellModels = [Model]()
  12. /// cell重用标识
  13. var cellReuseIdentifier: String?
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. }
  17. func numberOfSections(in tableView: UITableView) -> Int {
  18. return 1
  19. }
  20. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  21. return tableCellModels.count
  22. }
  23. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  24. assert(cellReuseIdentifier != nil, "请在子类中设置cellReuseIdentifier")
  25. /// 要求必须注册cell
  26. let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier!, for: indexPath) as! BaseTableViewCell<Model>
  27. cell.rowNum = indexPath.row
  28. cell.takeInfo = takeInfo
  29. cell.model = tableCellModels[indexPath.row]
  30. return cell
  31. }
  32. /// 注意,子类中需要用到的方法在这个类中都要定义,让子类覆盖的方式实现。否则,这个类没有实现的方法,子类不能覆盖,也不能继承!!!
  33. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  34. tableView.deselectRow(at: indexPath, animated: true)
  35. }
  36. /// 定义已便让子类覆盖
  37. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  38. return UITableView.automaticDimension
  39. }
  40. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  41. return nil
  42. }
  43. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  44. return 0
  45. }
  46. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  47. return 0
  48. }
  49. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  50. return nil
  51. }
  52. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {}
  53. func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {}
  54. func scrollViewDidScroll(_ scrollView: UIScrollView) {}
  55. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {}
  56. }
  57. protocol GroupedTableViewDelegate {
  58. associatedtype Models
  59. var sections: Int { get }
  60. var rowsInSection: [Int] { get }
  61. subscript(row: Int, section: Int) -> Models { get set }
  62. }
  63. class GroupedTableViewController<Model>: BaseTableViewController<Model>, GroupedTableViewDelegate {
  64. lazy var rowsInSection = [Int](repeating: 0, count: sections-1)
  65. lazy var sections = 0
  66. typealias Models = Model
  67. func indexIsValid(row: Int, section: Int) -> Bool {
  68. return row >= 0 && row < rowsInSection[section] && section >= 0 && section < sections
  69. }
  70. func getRowAndSection(index: Int) -> (row: Int, section: Int)? {
  71. guard index < tableCellModels.count else { return nil }
  72. var indexs = 0
  73. for i in 0 ..< sections {
  74. if index < indexs + rowsInSection[i] {
  75. return (row: index - indexs, section: i)
  76. }
  77. indexs += rowsInSection[i]
  78. }
  79. return nil
  80. }
  81. func getModel(index: Int) -> Model? {
  82. guard index < tableCellModels.count - 1 else { return nil }
  83. var indexs = 0
  84. for i in 0 ..< sections - 1 {
  85. if index < indexs + rowsInSection[i] {
  86. return self[index - indexs, i]
  87. }
  88. indexs += rowsInSection[i]
  89. }
  90. return nil
  91. }
  92. subscript(row: Int, section: Int) -> Model {
  93. get {
  94. return getModel(row: row, section: section)
  95. }
  96. set {
  97. setModel(row: row, section: section, newModel: newValue)
  98. }
  99. }
  100. private final func getModel(row: Int, section: Int) -> Model {
  101. var rows = 0
  102. for i in 0..<section {
  103. rows += rowsInSection[i]
  104. }
  105. return tableCellModels[rows + row]
  106. }
  107. private final func setModel(row: Int, section: Int, newModel: Model) {
  108. var rows = 0
  109. for i in 0..<section {
  110. rows += rowsInSection[i]
  111. }
  112. tableCellModels[rows + row] = newModel
  113. }
  114. override func numberOfSections(in tableView: UITableView) -> Int {
  115. return sections
  116. }
  117. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  118. return rowsInSection[section]
  119. }
  120. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  121. assert(cellReuseIdentifier != nil, "请在子类中设置cellReuseIdentifier")
  122. /// 要求必须注册cell
  123. let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier!, for: indexPath) as! BaseTableViewCell<Model>
  124. cell.rowNum = indexPath.row
  125. cell.takeInfo = takeInfo
  126. cell.model = getModel(row: indexPath.row, section: indexPath.section)
  127. return cell
  128. }
  129. }