Skip to content

Commit

Permalink
Slight fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hughbe committed Feb 5, 2018
1 parent 9c680ac commit bcbb7d3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion DayDatePicker.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'DayDatePicker'
s.version = '1.1'
s.version = '1.2'
s.summary = 'A custom and customizable UIDatePicker which displays the day of the week alongside the day column'

s.description = <<-DESC
Expand Down
21 changes: 9 additions & 12 deletions DayDatePicker/Classes/DayDatePickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,14 @@ public class DayDatePickerView : UIControl {
date = minTime
}

let reloadMonthView = _date.year != date.year
let reloadDayView = reloadMonthView || _date.month != date.month

let reloadMonthTableView = date.year != _date.year
let reloadDayTableView = reloadMonthTableView || date.month != _date.month
_date = date

if reloadMonthView {
if reloadMonthTableView {
monthTableView.reloadAndLayout()
}
if reloadDayView {
if reloadDayTableView {
dayTableView.reloadAndLayout()
}

Expand All @@ -82,11 +81,10 @@ public class DayDatePickerView : UIControl {

public func setMinDate(minDate: Date?, animated: Bool) {
_minDate = minDate

reload()

if let minDate = minDate, date < minDate {
setDate(year: minDate.year, month: minDate.month, day: minDate.day, animated: true)
setDate(date: minDate, animated: true)
}
}

Expand Down Expand Up @@ -175,9 +173,9 @@ extension DayDatePickerView {
_date = Date(year: components.year, month: components.month, day: components.day)
}

yearTableView.reloadData()
monthTableView.reloadData()
dayTableView.reloadData()
dayRange = Calendar.current.range(of: .day, in: .month, for: _date.date)
monthRange = Calendar.current.range(of: .month, in: .year, for: _date.date)
yearRange = Calendar.current.range(of: .year, in: .era, for: _date.date)

superview?.layoutIfNeeded()
setDate(date: _date, animated: false)
Expand Down Expand Up @@ -367,4 +365,3 @@ extension DayDatePickerView {
}
}
}

30 changes: 18 additions & 12 deletions DayDatePicker/Classes/TimePickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ public class TimePickerView : UIControl {
}

time.minute = time.minute.round(toNearest: minuteInterval)

let reloadMinuteView = _time.hour != time.hour
let reloadMinuteTableView = time.hour != _time.hour
_time = time

if reloadMinuteView {
if reloadMinuteTableView {
minuteTableView.reloadAndLayout()
}

Expand All @@ -72,11 +71,10 @@ public class TimePickerView : UIControl {

public func setMinTime(minTime: Time?, animated: Bool) {
_minTime = minTime

reload()

if let minTime = minTime, time < minTime {
setTime(hour: minTime.hour, minute: minTime.minute, animated: true)
setTime(time: minTime, animated: animated)
}
}

Expand All @@ -87,7 +85,7 @@ public class TimePickerView : UIControl {
precondition(newValue >= secondsInMinute.lowerBound && secondsInMinute.upperBound % newValue == 0, "The time interval has to be a positive number. 60 must be divisible by the interval.")
}
} didSet {
minuteTableView.reloadAndLayout()
minuteTableView.reloadData()
}
}

Expand Down Expand Up @@ -143,15 +141,15 @@ extension TimePickerView {
NSLayoutConstraint(item: overlayView, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: 0),
NSLayoutConstraint(item: overlayView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0),
NSLayoutConstraint(item: overlayView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: rowHeight)
])
])

if _time == nil {
let components = Calendar.current.dateComponents([.hour, .minute], from: Date()) as NSDateComponents
_time = Time(hour: components.hour, minute: components.minute)
}

hourTableView.reloadData()
minuteTableView.reloadData()
hourRange = Calendar.current.range(of: .hour, in: .day, for: Date())
minuteRange = Calendar.current.range(of: .minute, in: .hour, for: Date())

superview?.layoutIfNeeded()
setTime(time: _time, animated: false)
Expand All @@ -160,6 +158,7 @@ extension TimePickerView {
private func setupTableView(tableView: UITableView) {
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.rowHeight = rowHeight
tableView.estimatedRowHeight = rowHeight
tableView.showsVerticalScrollIndicator = false
tableView.separatorStyle = .none
tableView.backgroundColor = UIColor.white
Expand Down Expand Up @@ -226,6 +225,14 @@ extension TimePickerView : UITableViewDataSource, UITableViewDelegate {

return cell;
}

public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return rowHeight
}

public func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return rowHeight
}

public func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
guard let tableView = scrollView as? UITableView else {
Expand All @@ -249,7 +256,7 @@ extension TimePickerView : UITableViewDataSource, UITableViewDelegate {
let row = tableView.getRowScroll()

if tableView == hourTableView {
hour = hourRange.upperBound + row
hour = hourRange.lowerBound + row
} else if tableView == minuteTableView {
minute = minuteRange.lowerBound + row * minuteInterval
}
Expand Down Expand Up @@ -298,4 +305,3 @@ extension TimePickerView {
}
}
}

1 change: 1 addition & 0 deletions DayDatePicker/Classes/UITableView+ReloadAndLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import UIKit
internal extension UITableView {
func reloadAndLayout() {
reloadData()
setNeedsLayout()
layoutIfNeeded()
}

Expand Down

0 comments on commit bcbb7d3

Please sign in to comment.