TapActionRow.swift 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // TapActionRow.swift
  3. // QuickTableViewController
  4. //
  5. // Created by Ben on 01/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. /// A class that represents a row that triggers certain action when selected.
  28. open class TapActionRow<T: TapActionCell>: TapActionRowCompatible, Equatable {
  29. // MARK: - Initializer
  30. /// Initializes a `TapActionRow` with a text, an action closure,
  31. /// and an optional customization closure.
  32. public init(
  33. text: String,
  34. customization: ((UITableViewCell, Row & RowStyle) -> Void)? = nil,
  35. action: ((Row) -> Void)?
  36. ) {
  37. self.text = text
  38. self.customize = customization
  39. self.action = action
  40. }
  41. // MARK: - Row
  42. /// The text of the row.
  43. public let text: String
  44. /// The detail text is disabled in `TapActionRow`.
  45. public let detailText: DetailText? = nil
  46. /// A closure that will be invoked when the row is selected.
  47. public let action: ((Row) -> Void)?
  48. // MARK: - RowStyle
  49. /// The type of the table view cell to display the row.
  50. public let cellType: UITableViewCell.Type = T.self
  51. /// The reuse identifier of the table view cell to display the row. The default value is **TapActionCell**.
  52. public let cellReuseIdentifier: String = T.reuseIdentifier
  53. /// The cell style is `.default`.
  54. public let cellStyle: UITableViewCell.CellStyle = .default
  55. /// The default icon is nil.
  56. public let icon: Icon? = nil
  57. /// The default accessory type is `.none`.
  58. public let accessoryType: UITableViewCell.AccessoryType = .none
  59. /// The `TapActionRow` is selectable when action is not nil.
  60. public var isSelectable: Bool {
  61. return action != nil
  62. }
  63. /// The additional customization during cell configuration.
  64. public let customize: ((UITableViewCell, Row & RowStyle) -> Void)?
  65. // MARK: - Equatable
  66. /// Returns true iff `lhs` and `rhs` have equal titles and detail texts.
  67. public static func == (lhs: TapActionRow, rhs: TapActionRow) -> Bool {
  68. return
  69. lhs.text == rhs.text &&
  70. lhs.detailText == rhs.detailText
  71. }
  72. }