Cell.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Cell.swift
  2. // Eureka ( https://github.com/xmartlabs/Eureka )
  3. //
  4. // Copyright (c) 2016 Xmartlabs ( http://xmartlabs.com )
  5. //
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. import Foundation
  25. import UIKit
  26. /// Base class for the Eureka cells
  27. @objc(EurekaBaseCell)
  28. open class BaseCell: UITableViewCell, BaseCellType {
  29. /// Untyped row associated to this cell.
  30. public var baseRow: BaseRow! { return nil }
  31. /// Block that returns the height for this cell.
  32. public var height: (() -> CGFloat)?
  33. public required init?(coder aDecoder: NSCoder) {
  34. super.init(coder: aDecoder)
  35. }
  36. public required override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  37. super.init(style: style, reuseIdentifier: reuseIdentifier)
  38. }
  39. /**
  40. Function that returns the FormViewController this cell belongs to.
  41. */
  42. public func formViewController() -> FormViewController? {
  43. var responder: UIResponder? = self
  44. while responder != nil {
  45. if let formVC = responder as? FormViewController {
  46. return formVC
  47. }
  48. responder = responder?.next
  49. }
  50. return nil
  51. }
  52. open func setup() {}
  53. open func update() {}
  54. open func didSelect() {}
  55. /**
  56. If the cell can become first responder. By default returns false
  57. */
  58. open func cellCanBecomeFirstResponder() -> Bool {
  59. return false
  60. }
  61. /**
  62. Called when the cell becomes first responder
  63. */
  64. @discardableResult
  65. open func cellBecomeFirstResponder(withDirection: Direction = .down) -> Bool {
  66. return becomeFirstResponder()
  67. }
  68. /**
  69. Called when the cell resigns first responder
  70. */
  71. @discardableResult
  72. open func cellResignFirstResponder() -> Bool {
  73. return resignFirstResponder()
  74. }
  75. }
  76. /// Generic class that represents the Eureka cells.
  77. open class Cell<T>: BaseCell, TypedCellType where T: Equatable {
  78. public typealias Value = T
  79. /// The row associated to this cell
  80. public weak var row: RowOf<T>!
  81. private var updatingCellForTintColorDidChange = false
  82. /// Returns the navigationAccessoryView if it is defined or calls super if not.
  83. override open var inputAccessoryView: UIView? {
  84. if let v = formViewController()?.inputAccessoryView(for: row) {
  85. return v
  86. }
  87. return super.inputAccessoryView
  88. }
  89. required public init?(coder aDecoder: NSCoder) {
  90. super.init(coder: aDecoder)
  91. }
  92. required public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  93. super.init(style: style, reuseIdentifier: reuseIdentifier)
  94. }
  95. /**
  96. Function responsible for setting up the cell at creation time.
  97. */
  98. open override func setup() {
  99. super.setup()
  100. }
  101. /**
  102. Function responsible for updating the cell each time it is reloaded.
  103. */
  104. open override func update() {
  105. super.update()
  106. textLabel?.text = row.title
  107. if #available(iOS 13.0, *) {
  108. textLabel?.textColor = row.isDisabled ? .tertiaryLabel : .label
  109. } else {
  110. textLabel?.textColor = row.isDisabled ? .gray : .black
  111. }
  112. detailTextLabel?.text = row.displayValueFor?(row.value) ?? (row as? NoValueDisplayTextConformance)?.noValueDisplayText
  113. }
  114. /**
  115. Called when the cell was selected.
  116. */
  117. open override func didSelect() {}
  118. override open var canBecomeFirstResponder: Bool {
  119. return false
  120. }
  121. open override func becomeFirstResponder() -> Bool {
  122. let result = super.becomeFirstResponder()
  123. if result {
  124. formViewController()?.beginEditing(of: self)
  125. }
  126. return result
  127. }
  128. open override func resignFirstResponder() -> Bool {
  129. let result = super.resignFirstResponder()
  130. if result {
  131. formViewController()?.endEditing(of: self)
  132. }
  133. return result
  134. }
  135. open override func tintColorDidChange() {
  136. super.tintColorDidChange()
  137. /* Protection from infinite recursion in case an update method changes the tintColor */
  138. if !updatingCellForTintColorDidChange && row != nil {
  139. updatingCellForTintColorDidChange = true
  140. row.updateCell()
  141. updatingCellForTintColorDidChange = false
  142. }
  143. }
  144. /// The untyped row associated to this cell.
  145. public override var baseRow: BaseRow! { return row }
  146. }