SwitchRow.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // SwitchRow.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: SwitchCell
  27. open class SwitchCell: Cell<Bool>, CellType {
  28. @IBOutlet public weak var switchControl: UISwitch!
  29. required public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  30. super.init(style: style, reuseIdentifier: reuseIdentifier)
  31. let switchC = UISwitch()
  32. switchControl = switchC
  33. accessoryView = switchControl
  34. editingAccessoryView = accessoryView
  35. }
  36. required public init?(coder aDecoder: NSCoder) {
  37. super.init(coder: aDecoder)
  38. }
  39. open override func setup() {
  40. super.setup()
  41. selectionStyle = .none
  42. switchControl.addTarget(self, action: #selector(SwitchCell.valueChanged), for: .valueChanged)
  43. }
  44. deinit {
  45. switchControl?.removeTarget(self, action: nil, for: .allEvents)
  46. }
  47. open override func update() {
  48. super.update()
  49. switchControl.isOn = row.value ?? false
  50. switchControl.isEnabled = !row.isDisabled
  51. }
  52. @objc func valueChanged() {
  53. row.value = switchControl?.isOn ?? false
  54. }
  55. }
  56. // MARK: SwitchRow
  57. open class _SwitchRow: Row<SwitchCell> {
  58. required public init(tag: String?) {
  59. super.init(tag: tag)
  60. displayValueFor = nil
  61. }
  62. }
  63. /// Boolean row that has a UISwitch as accessoryType
  64. public final class SwitchRow: _SwitchRow, RowType {
  65. required public init(tag: String?) {
  66. super.init(tag: tag)
  67. }
  68. }