forked from DylanVann/DatePickerCell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDVDatePickerTableViewCell.swift
294 lines (261 loc) · 10 KB
/
DVDatePickerTableViewCell.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//
// DVDatePickerTableViewCell.swift
// DVDatePickerTableViewCellDemo
//
// Created by Dylan Vann on 2014-10-21.
// Copyright (c) 2014 Dylan Vann. All rights reserved.
//
import Foundation
import UIKit
// DVColorLockView ovverides the backgroundColor setter for UIView so that highlighting a UITableViewCell won't change the color of its DVColorLockView subviews (Highlighting a UITableViewCell changes the background color of all subviews, it's annoying.)
class DVColorLockView:UIView {
var lockedBackgroundColor:UIColor {
set {
super.backgroundColor = newValue
}
get {
return super.backgroundColor!
}
}
override var backgroundColor:UIColor? {
set {
}
get {
return super.backgroundColor
}
}
}
class DVDatePickerTableViewCell: UITableViewCell {
// Class variable workaround.
struct Stored {
static var dateFormatter = NSDateFormatter()
}
var date:NSDate = NSDate() {
didSet {
datePicker.date = date
DVDatePickerTableViewCell.Stored.dateFormatter.dateStyle = dateStyle
DVDatePickerTableViewCell.Stored.dateFormatter.timeStyle = timeStyle
rightLabel.text = DVDatePickerTableViewCell.Stored.dateFormatter.stringFromDate(date)
}
}
var timeStyle = NSDateFormatterStyle.ShortStyle, dateStyle = NSDateFormatterStyle.ShortStyle
var leftLabel = UILabel(), rightLabel = UILabel()
var rightLabelTextColor = UIColor(hue: 0.639, saturation: 0.041, brightness: 0.576, alpha: 1.0) //Color of normal detail label.
var seperator = DVColorLockView()
var datePickerContainer = UIView()
var datePicker: UIDatePicker = UIDatePicker()
var expanded = false
var unexpandedHeight = CGFloat(44)
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setup()
}
private func setup() {
// The datePicker overhangs the view slightly to avoid invalid constraints.
self.clipsToBounds = true
var views = [leftLabel, rightLabel, seperator, datePickerContainer, datePicker]
for view in views {
self.contentView .addSubview(view)
view.setTranslatesAutoresizingMaskIntoConstraints(false)
}
datePickerContainer.clipsToBounds = true
datePickerContainer.addSubview(datePicker)
// Add a seperator between the date text display, and the datePicker. Lighter grey than a normal seperator.
seperator.lockedBackgroundColor = UIColor(white: 0, alpha: 0.1)
datePickerContainer.addSubview(seperator)
datePickerContainer.addConstraints([
NSLayoutConstraint(
item: seperator,
attribute: NSLayoutAttribute.Left,
relatedBy: NSLayoutRelation.Equal,
toItem: datePickerContainer,
attribute: NSLayoutAttribute.Left,
multiplier: 1.0,
constant: 0
),
NSLayoutConstraint(
item: seperator,
attribute: NSLayoutAttribute.Right,
relatedBy: NSLayoutRelation.Equal,
toItem: datePickerContainer,
attribute: NSLayoutAttribute.Right,
multiplier: 1.0,
constant: 0
),
NSLayoutConstraint(
item: seperator,
attribute: NSLayoutAttribute.Height,
relatedBy: NSLayoutRelation.Equal,
toItem: nil,
attribute: NSLayoutAttribute.NotAnAttribute,
multiplier: 1.0,
constant: 0.5
),
NSLayoutConstraint(
item: seperator,
attribute: NSLayoutAttribute.Top,
relatedBy: NSLayoutRelation.Equal,
toItem: datePickerContainer,
attribute: NSLayoutAttribute.Top,
multiplier: 1.0,
constant: 0
),
])
rightLabel.textColor = rightLabelTextColor
//Left label.
self.contentView.addConstraints([
NSLayoutConstraint(
item: leftLabel,
attribute: NSLayoutAttribute.Height,
relatedBy: NSLayoutRelation.Equal,
toItem: nil,
attribute: NSLayoutAttribute.NotAnAttribute,
multiplier: 1.0,
constant: 44
),
NSLayoutConstraint(
item: leftLabel,
attribute: NSLayoutAttribute.Top,
relatedBy: NSLayoutRelation.Equal,
toItem: self.contentView,
attribute: NSLayoutAttribute.Top,
multiplier: 1.0,
constant: 0
),
NSLayoutConstraint(
item: leftLabel,
attribute: NSLayoutAttribute.Left,
relatedBy: NSLayoutRelation.Equal,
toItem: self.contentView,
attribute: NSLayoutAttribute.Left,
multiplier: 1.0,
constant: self.separatorInset.left
),
])
//Right label
self.contentView.addConstraints([
NSLayoutConstraint(
item: rightLabel,
attribute: NSLayoutAttribute.Height,
relatedBy: NSLayoutRelation.Equal,
toItem: nil,
attribute: NSLayoutAttribute.NotAnAttribute,
multiplier: 1.0,
constant: 44
),
NSLayoutConstraint(
item: rightLabel,
attribute: NSLayoutAttribute.Top,
relatedBy: NSLayoutRelation.Equal,
toItem: self.contentView,
attribute: NSLayoutAttribute.Top,
multiplier: 1.0,
constant: 0
),
NSLayoutConstraint(
item: rightLabel,
attribute: NSLayoutAttribute.Right,
relatedBy: NSLayoutRelation.Equal,
toItem: self.contentView,
attribute: NSLayoutAttribute.Right,
multiplier: 1.0,
constant: -self.separatorInset.left
),
])
// Container.
self.contentView.addConstraints([
NSLayoutConstraint(
item: datePickerContainer,
attribute: NSLayoutAttribute.Left,
relatedBy: NSLayoutRelation.Equal,
toItem: self.contentView,
attribute: NSLayoutAttribute.Left,
multiplier: 1.0,
constant: 0
),
NSLayoutConstraint(
item: datePickerContainer,
attribute: NSLayoutAttribute.Right,
relatedBy: NSLayoutRelation.Equal,
toItem: self.contentView,
attribute: NSLayoutAttribute.Right,
multiplier: 1.0,
constant: 0
),
NSLayoutConstraint(
item: datePickerContainer,
attribute: NSLayoutAttribute.Top,
relatedBy: NSLayoutRelation.Equal,
toItem: leftLabel,
attribute: NSLayoutAttribute.Bottom,
multiplier: 1.0,
constant: 0
),
NSLayoutConstraint(
item: datePickerContainer,
attribute: NSLayoutAttribute.Bottom,
relatedBy: NSLayoutRelation.Equal,
toItem: self.contentView,
attribute: NSLayoutAttribute.Bottom,
multiplier: 1.0,
constant: 1
),
])
// Picker constraints.
datePickerContainer.addConstraints([
NSLayoutConstraint(
item: datePicker,
attribute: NSLayoutAttribute.Left,
relatedBy: NSLayoutRelation.Equal,
toItem: datePickerContainer,
attribute: NSLayoutAttribute.Left,
multiplier: 1.0,
constant: 0
),
NSLayoutConstraint(
item: datePicker,
attribute: NSLayoutAttribute.Right,
relatedBy: NSLayoutRelation.Equal,
toItem: datePickerContainer,
attribute: NSLayoutAttribute.Right,
multiplier: 1.0,
constant: 0
),
NSLayoutConstraint(
item: datePicker,
attribute: NSLayoutAttribute.CenterY,
relatedBy: NSLayoutRelation.Equal,
toItem: datePickerContainer,
attribute: NSLayoutAttribute.CenterY,
multiplier: 1.0,
constant: 0
),
])
datePicker.addTarget(self, action: "datePicked", forControlEvents: UIControlEvents.ValueChanged)
setDate(NSDate())
leftLabel.text = "Date Picker"
}
// didSet isn't called within init. Use this.
func setDate(date: NSDate) {
self.date = date
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func datePickerHeight() -> CGFloat {
var expandedHeight = unexpandedHeight + CGFloat(datePicker.frame.size.height)
return expanded ? expandedHeight : unexpandedHeight
}
func selectedInTableView(tableView: UITableView) {
expanded = !expanded
UIView.transitionWithView(rightLabel, duration: 0.25, options:UIViewAnimationOptions.TransitionCrossDissolve, animations: { () -> Void in
self.rightLabel.textColor = self.expanded ? self.tintColor : self.rightLabelTextColor
}, completion: nil)
tableView.beginUpdates()
tableView.endUpdates()
}
func datePicked() {
date = datePicker.date
}
}