SwitchCell.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // SwitchCell.swift
  3. // QuickTableViewController
  4. //
  5. // Created by Ben on 03/09/2015.
  6. // Copyright (c) 2015 bcylin.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in all
  16. // copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. // SOFTWARE.
  25. //
  26. import UIKit
  27. /// The `SwitchCellDelegate` protocol allows the adopting delegate to respond to the UI interaction. Not available on tvOS.
  28. @available(tvOS, unavailable, message: "SwitchCellDelegate is not available on tvOS.")
  29. public protocol SwitchCellDelegate: class {
  30. /// Tells the delegate that the switch control is toggled.
  31. func switchCell(_ cell: SwitchCell, didToggleSwitch isOn: Bool)
  32. }
  33. /// A `UITableViewCell` subclass that shows a `UISwitch` as the `accessoryView`.
  34. open class SwitchCell: UITableViewCell, Configurable {
  35. #if os(iOS)
  36. /// A `UISwitch` as the `accessoryView`. Not available on tvOS.
  37. @available(tvOS, unavailable, message: "switchControl is not available on tvOS.")
  38. public private(set) lazy var switchControl: UISwitch = {
  39. let control = UISwitch()
  40. control.addTarget(self, action: #selector(SwitchCell.didToggleSwitch(_:)), for: .valueChanged)
  41. return control
  42. }()
  43. #endif
  44. /// The switch cell's delegate object, which should conform to `SwitchCellDelegate`. Not available on tvOS.
  45. @available(tvOS, unavailable, message: "SwitchCellDelegate is not available on tvOS.")
  46. open weak var delegate: SwitchCellDelegate?
  47. // MARK: - Initializer
  48. /**
  49. Overrides `UITableViewCell`'s designated initializer.
  50. - parameter style: A constant indicating a cell style.
  51. - parameter reuseIdentifier: A string used to identify the cell object if it is to be reused for drawing multiple rows of a table view.
  52. - returns: An initialized `SwitchCell` object.
  53. */
  54. public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  55. super.init(style: style, reuseIdentifier: reuseIdentifier)
  56. setUpAppearance()
  57. }
  58. /**
  59. Overrides the designated initializer that returns an object initialized from data in a given unarchiver.
  60. - parameter aDecoder: An unarchiver object.
  61. - returns: `self`, initialized using the data in decoder.
  62. */
  63. public required init?(coder aDecoder: NSCoder) {
  64. super.init(coder: aDecoder)
  65. setUpAppearance()
  66. }
  67. // MARK: - Configurable
  68. /// Set up the switch control (iOS) or accessory type (tvOS) with the provided row.
  69. open func configure(with row: Row & RowStyle) {
  70. #if os(iOS)
  71. if let row = row as? SwitchRowCompatible {
  72. switchControl.isOn = row.switchValue
  73. }
  74. accessoryView = switchControl
  75. #elseif os(tvOS)
  76. accessoryView = nil
  77. accessoryType = row.accessoryType
  78. #endif
  79. }
  80. // MARK: - Private
  81. @available(tvOS, unavailable, message: "UISwitch is not available on tvOS.")
  82. @objc
  83. private func didToggleSwitch(_ sender: UISwitch) {
  84. delegate?.switchCell(self, didToggleSwitch: sender.isOn)
  85. }
  86. private func setUpAppearance() {
  87. textLabel?.numberOfLines = 0
  88. detailTextLabel?.numberOfLines = 0
  89. }
  90. }