PickerRow.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // PickerRow.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: PickerCell
  27. open class _PickerCell<T> : Cell<T>, CellType, UIPickerViewDataSource, UIPickerViewDelegate where T: Equatable {
  28. @IBOutlet public weak var picker: UIPickerView!
  29. fileprivate var pickerRow: _PickerRow<T>? { return row as? _PickerRow<T> }
  30. public required init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  31. let pickerView = UIPickerView()
  32. self.picker = pickerView
  33. self.picker?.translatesAutoresizingMaskIntoConstraints = false
  34. super.init(style: style, reuseIdentifier: reuseIdentifier)
  35. self.contentView.addSubview(pickerView)
  36. self.contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[picker]-0-|", options: [], metrics: nil, views: ["picker": pickerView]))
  37. self.contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[picker]-0-|", options: [], metrics: nil, views: ["picker": pickerView]))
  38. }
  39. required public init?(coder aDecoder: NSCoder) {
  40. super.init(coder: aDecoder)
  41. }
  42. open override func setup() {
  43. super.setup()
  44. accessoryType = .none
  45. editingAccessoryType = .none
  46. height = { UITableView.automaticDimension }
  47. picker.delegate = self
  48. picker.dataSource = self
  49. }
  50. open override func update() {
  51. super.update()
  52. textLabel?.text = nil
  53. detailTextLabel?.text = nil
  54. picker.reloadAllComponents()
  55. }
  56. deinit {
  57. picker?.delegate = nil
  58. picker?.dataSource = nil
  59. }
  60. open var pickerTextAttributes: [NSAttributedString.Key: Any]?
  61. open func numberOfComponents(in pickerView: UIPickerView) -> Int {
  62. return 1
  63. }
  64. open func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  65. return pickerRow?.options.count ?? 0
  66. }
  67. open func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  68. return pickerRow?.displayValueFor?(pickerRow?.options[row])
  69. }
  70. open func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  71. if let picker = pickerRow, !picker.options.isEmpty {
  72. picker.value = picker.options[row]
  73. }
  74. }
  75. open func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
  76. guard let pickerTextAttributes = pickerTextAttributes, let text = self.pickerView(pickerView, titleForRow: row, forComponent: component) else {
  77. return nil
  78. }
  79. return NSAttributedString(string: text, attributes: pickerTextAttributes)
  80. }
  81. }
  82. open class PickerCell<T> : _PickerCell<T> where T: Equatable {
  83. required public init?(coder aDecoder: NSCoder) {
  84. super.init(coder: aDecoder)
  85. }
  86. public required init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  87. super.init(style: style, reuseIdentifier: reuseIdentifier)
  88. }
  89. open override func update() {
  90. super.update()
  91. if let selectedValue = pickerRow?.value, let index = pickerRow?.options.firstIndex(of: selectedValue) {
  92. picker.selectRow(index, inComponent: 0, animated: true)
  93. }
  94. }
  95. }
  96. // MARK: PickerRow
  97. open class _PickerRow<T> : Row<PickerCell<T>> where T: Equatable {
  98. open var options = [T]()
  99. required public init(tag: String?) {
  100. super.init(tag: tag)
  101. }
  102. }
  103. /// A generic row where the user can pick an option from a picker view
  104. public final class PickerRow<T>: _PickerRow<T>, RowType where T: Equatable {
  105. required public init(tag: String?) {
  106. super.init(tag: tag)
  107. }
  108. }