PickerInputRow.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // PickerInputRow.swift
  2. // Eureka ( https://github.com/xmartlabs/Eureka )
  3. //
  4. // Copyright (c) 2016 Xmartlabs SRL ( 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. // MARK: PickerInputCell
  27. open class _PickerInputCell<T> : Cell<T>, CellType, UIPickerViewDataSource, UIPickerViewDelegate where T: Equatable {
  28. lazy public var picker: UIPickerView = {
  29. let picker = UIPickerView()
  30. picker.translatesAutoresizingMaskIntoConstraints = false
  31. return picker
  32. }()
  33. fileprivate var pickerInputRow: _PickerInputRow<T>? { return row as? _PickerInputRow<T> }
  34. public required init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  35. super.init(style: style, reuseIdentifier: reuseIdentifier)
  36. }
  37. required public init?(coder aDecoder: NSCoder) {
  38. super.init(coder: aDecoder)
  39. }
  40. open override func setup() {
  41. super.setup()
  42. accessoryType = .none
  43. editingAccessoryType = .none
  44. picker.delegate = self
  45. picker.dataSource = self
  46. }
  47. deinit {
  48. picker.delegate = nil
  49. picker.dataSource = nil
  50. }
  51. open override func update() {
  52. super.update()
  53. selectionStyle = row.isDisabled ? .none : .default
  54. if row.title?.isEmpty == false {
  55. detailTextLabel?.text = row.displayValueFor?(row.value) ?? (row as? NoValueDisplayTextConformance)?.noValueDisplayText
  56. } else {
  57. textLabel?.text = row.displayValueFor?(row.value) ?? (row as? NoValueDisplayTextConformance)?.noValueDisplayText
  58. detailTextLabel?.text = nil
  59. }
  60. if #available(iOS 13.0, *) {
  61. textLabel?.textColor = row.isDisabled ? .tertiaryLabel : .label
  62. } else {
  63. textLabel?.textColor = row.isDisabled ? .gray : .black
  64. }
  65. if row.isHighlighted {
  66. textLabel?.textColor = tintColor
  67. }
  68. picker.reloadAllComponents()
  69. }
  70. open override func didSelect() {
  71. super.didSelect()
  72. row.deselect()
  73. }
  74. open override var inputView: UIView? {
  75. return picker
  76. }
  77. open override func cellCanBecomeFirstResponder() -> Bool {
  78. return canBecomeFirstResponder
  79. }
  80. override open var canBecomeFirstResponder: Bool {
  81. return !row.isDisabled
  82. }
  83. open func numberOfComponents(in pickerView: UIPickerView) -> Int {
  84. return 1
  85. }
  86. open func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  87. return pickerInputRow?.options.count ?? 0
  88. }
  89. open func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  90. return pickerInputRow?.displayValueFor?(pickerInputRow?.options[row])
  91. }
  92. open func pickerView(_ pickerView: UIPickerView, didSelectRow rowNumber: Int, inComponent component: Int) {
  93. if let picker = pickerInputRow, picker.options.count > rowNumber {
  94. picker.value = picker.options[rowNumber]
  95. update()
  96. }
  97. }
  98. }
  99. open class PickerInputCell<T>: _PickerInputCell<T> where T: Equatable {
  100. public required init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  101. super.init(style: style, reuseIdentifier: reuseIdentifier)
  102. }
  103. required public init?(coder aDecoder: NSCoder) {
  104. super.init(coder: aDecoder)
  105. }
  106. open override func update() {
  107. super.update()
  108. if let selectedValue = pickerInputRow?.value, let index = pickerInputRow?.options.firstIndex(of: selectedValue) {
  109. picker.selectRow(index, inComponent: 0, animated: true)
  110. }
  111. }
  112. }
  113. // MARK: PickerInputRow
  114. open class _PickerInputRow<T> : Row<PickerInputCell<T>>, NoValueDisplayTextConformance where T: Equatable {
  115. open var noValueDisplayText: String? = nil
  116. open var options = [T]()
  117. required public init(tag: String?) {
  118. super.init(tag: tag)
  119. }
  120. }
  121. /// A generic row where the user can pick an option from a picker view displayed in the keyboard area
  122. public final class PickerInputRow<T>: _PickerInputRow<T>, RowType where T: Equatable {
  123. required public init(tag: String?) {
  124. super.init(tag: tag)
  125. }
  126. }