BaseTableViewController.swift 5.9 KB

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