QuickTableViewController.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // QuickTableViewController.swift
  3. // QuickTableViewController
  4. //
  5. // Created by Ben on 25/08/2015.
  6. // Copyright (c) 2015 bcylin.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in all
  16. // copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. // SOFTWARE.
  25. //
  26. import UIKit
  27. /// A table view controller that shows `tableContents` as formatted sections and rows.
  28. open class QuickTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
  29. /// A Boolean value indicating if the controller clears the selection when the collection view appears.
  30. open var clearsSelectionOnViewWillAppear = true
  31. /// Returns the table view managed by the controller object.
  32. open private(set) var tableView: UITableView = UITableView(frame: .zero, style: .grouped)
  33. /// The layout of sections and rows to display in the table view.
  34. open var tableContents: [Section] = [] {
  35. didSet {
  36. tableView.reloadData()
  37. }
  38. }
  39. // MARK: - Initialization
  40. /**
  41. Initializes a table view controller to manage a table view of a given style.
  42. - parameter style: A constant that specifies the style of table view that the controller object is to manage (`.plain` or `.grouped`).
  43. - returns: An initialized `QuickTableViewController` object.
  44. */
  45. public convenience init(style: UITableView.Style) {
  46. self.init(nibName: nil, bundle: nil)
  47. tableView = UITableView(frame: .zero, style: style)
  48. }
  49. deinit {
  50. tableView.dataSource = nil
  51. tableView.delegate = nil
  52. }
  53. // MARK: - UIViewController
  54. open override func viewDidLoad() {
  55. super.viewDidLoad()
  56. view.addSubview(tableView)
  57. tableView.frame = view.bounds
  58. tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  59. tableView.rowHeight = UITableView.automaticDimension
  60. tableView.estimatedRowHeight = 44
  61. tableView.dataSource = self
  62. tableView.delegate = self
  63. }
  64. open override func viewWillAppear(_ animated: Bool) {
  65. super.viewWillAppear(animated)
  66. if let indexPath = tableView.indexPathForSelectedRow, clearsSelectionOnViewWillAppear {
  67. tableView.deselectRow(at: indexPath, animated: true)
  68. }
  69. }
  70. // MARK: - UITableViewDataSource
  71. open func numberOfSections(in tableView: UITableView) -> Int {
  72. return tableContents.count
  73. }
  74. open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  75. return tableContents[section].rows.count
  76. }
  77. open func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  78. return tableContents[section].title
  79. }
  80. open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  81. let row = tableContents[indexPath.section].rows[indexPath.row]
  82. let cell =
  83. tableView.dequeueReusableCell(withIdentifier: row.cellReuseIdentifier) ??
  84. row.cellType.init(style: row.cellStyle, reuseIdentifier: row.cellReuseIdentifier)
  85. cell.defaultSetUp(with: row)
  86. (cell as? Configurable)?.configure(with: row)
  87. #if os(iOS)
  88. (cell as? SwitchCell)?.delegate = self
  89. #endif
  90. row.customize?(cell, row)
  91. return cell
  92. }
  93. open func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
  94. return tableContents[section].footer
  95. }
  96. // MARK: - UITableViewDelegate
  97. open func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
  98. return tableContents[indexPath.section].rows[indexPath.row].isSelectable
  99. }
  100. open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  101. let section = tableContents[indexPath.section]
  102. let row = section.rows[indexPath.row]
  103. switch (section, row) {
  104. case let (radio as RadioSection, option as OptionRowCompatible):
  105. let changes: [IndexPath] = radio.toggle(option).map {
  106. IndexPath(row: $0, section: indexPath.section)
  107. }
  108. if changes.isEmpty {
  109. tableView.deselectRow(at: indexPath, animated: false)
  110. } else {
  111. tableView.reloadRows(at: changes, with: .automatic)
  112. }
  113. case let (_, option as OptionRowCompatible):
  114. // Allow OptionRow to be used without RadioSection.
  115. option.isSelected = !option.isSelected
  116. tableView.reloadData()
  117. #if os(tvOS)
  118. case let (_, row as SwitchRowCompatible):
  119. // SwitchRow on tvOS behaves like OptionRow.
  120. row.switchValue = !row.switchValue
  121. tableView.reloadData()
  122. #endif
  123. case (_, is TapActionRowCompatible):
  124. tableView.deselectRow(at: indexPath, animated: true)
  125. // Avoid some unwanted animation when the action also involves table view reload.
  126. DispatchQueue.main.async {
  127. row.action?(row)
  128. }
  129. case let (_, row) where row.isSelectable:
  130. DispatchQueue.main.async {
  131. row.action?(row)
  132. }
  133. default:
  134. break
  135. }
  136. }
  137. #if os(iOS)
  138. public func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
  139. switch tableContents[indexPath.section].rows[indexPath.row] {
  140. case let row as NavigationRowCompatible:
  141. DispatchQueue.main.async {
  142. row.accessoryButtonAction?(row)
  143. }
  144. default:
  145. break
  146. }
  147. }
  148. #endif
  149. }
  150. #if os(iOS)
  151. extension QuickTableViewController: SwitchCellDelegate {
  152. // MARK: - SwitchCellDelegate
  153. open func switchCell(_ cell: SwitchCell, didToggleSwitch isOn: Bool) {
  154. guard
  155. let indexPath = tableView.indexPath(for: cell),
  156. let row = tableContents[indexPath.section].rows[indexPath.row] as? SwitchRowCompatible
  157. else {
  158. return
  159. }
  160. row.switchValue = isOn
  161. }
  162. }
  163. #endif