Cell.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. open class BaseCell: UITableViewCell, BaseCellType {
  28. /// Untyped row associated to this cell.
  29. public var baseRow: BaseRow! { return nil }
  30. /// Block that returns the height for this cell.
  31. public var height: (() -> CGFloat)?
  32. public required init?(coder aDecoder: NSCoder) {
  33. super.init(coder: aDecoder)
  34. }
  35. public required override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  36. super.init(style: style, reuseIdentifier: reuseIdentifier)
  37. }
  38. /**
  39. Function that returns the FormViewController this cell belongs to.
  40. */
  41. public func formViewController() -> FormViewController? {
  42. var responder: AnyObject? = self
  43. while responder != nil {
  44. if let formVC = responder as? FormViewController {
  45. return formVC
  46. }
  47. responder = responder?.next
  48. }
  49. return nil
  50. }
  51. open func setup() {}
  52. open func update() {}
  53. open func didSelect() {}
  54. /**
  55. If the cell can become first responder. By default returns false
  56. */
  57. open func cellCanBecomeFirstResponder() -> Bool {
  58. return false
  59. }
  60. /**
  61. Called when the cell becomes first responder
  62. */
  63. @discardableResult
  64. open func cellBecomeFirstResponder(withDirection: Direction = .down) -> Bool {
  65. return becomeFirstResponder()
  66. }
  67. /**
  68. Called when the cell resigns first responder
  69. */
  70. @discardableResult
  71. open func cellResignFirstResponder() -> Bool {
  72. return resignFirstResponder()
  73. }
  74. }
  75. /// Generic class that represents the Eureka cells.
  76. open class Cell<T>: BaseCell, TypedCellType where T: Equatable {
  77. public typealias Value = T
  78. /// The row associated to this cell
  79. public weak var row: RowOf<T>!
  80. private var updatingCellForTintColorDidChange = false
  81. /// Returns the navigationAccessoryView if it is defined or calls super if not.
  82. override open var inputAccessoryView: UIView? {
  83. if let v = formViewController()?.inputAccessoryView(for: row) {
  84. return v
  85. }
  86. return super.inputAccessoryView
  87. }
  88. required public init?(coder aDecoder: NSCoder) {
  89. super.init(coder: aDecoder)
  90. }
  91. required public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  92. super.init(style: style, reuseIdentifier: reuseIdentifier)
  93. }
  94. /**
  95. Function responsible for setting up the cell at creation time.
  96. */
  97. open override func setup() {
  98. super.setup()
  99. }
  100. /**
  101. Function responsible for updating the cell each time it is reloaded.
  102. */
  103. open override func update() {
  104. super.update()
  105. textLabel?.text = row.title
  106. if #available(iOS 13.0, *) {
  107. textLabel?.textColor = row.isDisabled ? .tertiaryLabel : .label
  108. } else {
  109. textLabel?.textColor = row.isDisabled ? .gray : .black
  110. }
  111. detailTextLabel?.text = row.displayValueFor?(row.value) ?? (row as? NoValueDisplayTextConformance)?.noValueDisplayText
  112. }
  113. /**
  114. Called when the cell was selected.
  115. */
  116. open override func didSelect() {}
  117. override open var canBecomeFirstResponder: Bool {
  118. return false
  119. }
  120. open override func becomeFirstResponder() -> Bool {
  121. let result = super.becomeFirstResponder()
  122. if result {
  123. formViewController()?.beginEditing(of: self)
  124. }
  125. return result
  126. }
  127. open override func resignFirstResponder() -> Bool {
  128. let result = super.resignFirstResponder()
  129. if result {
  130. formViewController()?.endEditing(of: self)
  131. }
  132. return result
  133. }
  134. open override func tintColorDidChange() {
  135. super.tintColorDidChange()
  136. /* Protection from infinite recursion in case an update method changes the tintColor */
  137. if !updatingCellForTintColorDidChange && row != nil {
  138. updatingCellForTintColorDidChange = true
  139. row.updateCell()
  140. updatingCellForTintColorDidChange = false
  141. }
  142. }
  143. /// The untyped row associated to this cell.
  144. public override var baseRow: BaseRow! { return row }
  145. }