DatePickerTableViewCell.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // DatePickerTableViewCell.swift
  3. // CarbKit
  4. //
  5. // Created by Nathan Racklyeft on 1/15/16.
  6. // Copyright © 2016 Nathan Racklyeft. All rights reserved.
  7. //
  8. import UIKit
  9. public protocol DatePickerTableViewCellDelegate: class {
  10. func datePickerTableViewCellDidUpdateDate(_ cell: DatePickerTableViewCell)
  11. }
  12. open class DatePickerTableViewCell: UITableViewCell {
  13. open var date: Date {
  14. get {
  15. return datePicker.date
  16. }
  17. set {
  18. datePicker.setDate(newValue, animated: true)
  19. updateDateLabel()
  20. }
  21. }
  22. open var duration: TimeInterval {
  23. get {
  24. return datePicker.countDownDuration
  25. }
  26. set {
  27. datePicker.countDownDuration = newValue
  28. updateDateLabel()
  29. }
  30. }
  31. open var maximumDuration = TimeInterval(hours: 8) {
  32. didSet {
  33. if duration > maximumDuration {
  34. duration = maximumDuration
  35. }
  36. }
  37. }
  38. @IBOutlet open weak var datePicker: UIDatePicker!
  39. @IBOutlet open weak var datePickerHeightConstraint: NSLayoutConstraint!
  40. private var datePickerExpandedHeight: CGFloat = 0
  41. open var isDatePickerHidden: Bool {
  42. get {
  43. return datePicker.isHidden || !datePicker.isEnabled
  44. }
  45. set {
  46. if datePicker.isEnabled {
  47. datePicker.isHidden = newValue
  48. datePickerHeightConstraint.constant = newValue ? 0 : datePickerExpandedHeight
  49. if !newValue, case .countDownTimer = datePicker.datePickerMode {
  50. // Workaround for target-action change notifications not firing if initial value is set while view is hidden
  51. DispatchQueue.main.async {
  52. self.datePicker.date = self.datePicker.date
  53. self.datePicker.countDownDuration = self.datePicker.countDownDuration
  54. }
  55. }
  56. }
  57. }
  58. }
  59. open override func awakeFromNib() {
  60. super.awakeFromNib()
  61. datePickerExpandedHeight = datePickerHeightConstraint.constant
  62. setSelected(true, animated: false)
  63. updateDateLabel()
  64. }
  65. open override func setSelected(_ selected: Bool, animated: Bool) {
  66. super.setSelected(selected, animated: animated)
  67. if selected {
  68. isDatePickerHidden = !isDatePickerHidden
  69. }
  70. }
  71. open func updateDateLabel() {
  72. }
  73. @IBAction open func dateChanged(_ sender: UIDatePicker) {
  74. if case .countDownTimer = sender.datePickerMode, duration > maximumDuration {
  75. duration = maximumDuration
  76. } else {
  77. updateDateLabel()
  78. }
  79. }
  80. }
  81. /// UITableViewController extensions to aid working with DatePickerTableViewCell
  82. extension DatePickerTableViewCellDelegate where Self: UITableViewController {
  83. public func hideDatePickerCells(excluding indexPath: IndexPath? = nil) {
  84. guard isViewLoaded else {
  85. return
  86. }
  87. for case let cell as DatePickerTableViewCell in tableView.visibleCells where tableView.indexPath(for: cell) != indexPath && cell.isDatePickerHidden == false {
  88. cell.isDatePickerHidden = true
  89. }
  90. }
  91. }