回答編集履歴
2
追記
test
CHANGED
@@ -15,3 +15,255 @@
|
|
15
15
|
|
16
16
|
|
17
17
|
[【swift】UIDatePickerを作ってみる(日付選択ドラムロール)](http://egao.blog/programming/swift_uidatepicker)
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
### 追記
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
customCellが作れるのであればunwind時に、つまりTableViewControllerに戻るときにlabel.textを埋める関数を呼び出せばいいです。
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
これはUITableViewをDynamic Prototypesで、なおかつUITableViewCellの中に置くTextをUILabelでやっています。ちなみにTableViewのUIが固定であればStatic Cellsを使って、なおかつUILabelにこだわりが無ければ(つまりUITextFieldを使えば)1/4の時間で出来上がります(というか出来上がった)。
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
Storyboard上でいくつか設定があります。多分先にコードを読んだ方がいいでしょう。
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
まずTableViewControllerとは別にViewControllerを配置。そのViewControllerにDatePickerとToolBarを配置、適切なConstraintsを設定。
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
各ViewControllerのCustomClassを設定。
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
TableViewController上の任意のCellのCustomClassをCustomCellに設定。
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
CustomClassがPickerViewControllerであるViewControllerのAttributes Inspector内にあるPresentationをOver Current Contextに設定。
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
unwindの設定。
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
以下コード
|
62
|
+
|
63
|
+
CustomCell.swift
|
64
|
+
|
65
|
+
```swift
|
66
|
+
|
67
|
+
import UIKit
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
class CustomCell: UITableViewCell {
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
@IBOutlet var dateLabel: UILabel!
|
76
|
+
|
77
|
+
override func awakeFromNib() {
|
78
|
+
|
79
|
+
super.awakeFromNib()
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
override func setSelected(_ selected: Bool, animated: Bool) {
|
86
|
+
|
87
|
+
super.setSelected(selected, animated: animated)
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
func fill(with text: String) {
|
94
|
+
|
95
|
+
dateLabel.text = text
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
```
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
PickerViewController.swift
|
108
|
+
|
109
|
+
```swift
|
110
|
+
|
111
|
+
import UIKit
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
class PickerViewController: UIViewController {
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
@IBOutlet var datePicker: UIDatePicker!
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
override func viewDidLoad() {
|
124
|
+
|
125
|
+
super.viewDidLoad()
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
override func didReceiveMemoryWarning() {
|
132
|
+
|
133
|
+
super.didReceiveMemoryWarning()
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
```
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
TableViewController.swift
|
146
|
+
|
147
|
+
```swift
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
import UIKit
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
class TableViewController: UITableViewController {
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
@IBAction func unwind(segue: UIStoryboardSegue) {
|
160
|
+
|
161
|
+
guard let source = segue.source as? PickerViewController else {
|
162
|
+
|
163
|
+
fatalError()
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
let date = source.datePicker.date
|
168
|
+
|
169
|
+
guard let indexPath = tableView.indexPathForSelectedRow else {
|
170
|
+
|
171
|
+
fatalError()
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
tableView.deselectRow(at: indexPath, animated: true)
|
176
|
+
|
177
|
+
guard let cell = tableView.cellForRow(at: indexPath) as? CustomCell else {
|
178
|
+
|
179
|
+
fatalError()
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
cell.fill(with: date.description)
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
override func viewDidLoad() {
|
194
|
+
|
195
|
+
super.viewDidLoad()
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
override func didReceiveMemoryWarning() {
|
202
|
+
|
203
|
+
super.didReceiveMemoryWarning()
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
// MARK: - Table view data source
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
override func numberOfSections(in tableView: UITableView) -> Int {
|
214
|
+
|
215
|
+
return 3
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
222
|
+
|
223
|
+
return 2
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
switch indexPath {
|
236
|
+
|
237
|
+
case IndexPath(row: 0, section: 0):
|
238
|
+
|
239
|
+
guard let cell = tableView.dequeueReusableCell(withIdentifier: "CUSTOMCELL", for: IndexPath(row: 0, section: 0)) as? CustomCell else {
|
240
|
+
|
241
|
+
fatalError()
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
return cell
|
246
|
+
|
247
|
+
default:
|
248
|
+
|
249
|
+
guard let cell = tableView.dequeueReusableCell(withIdentifier: "NORMALCELL") else {
|
250
|
+
|
251
|
+
fatalError()
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
return cell
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
```
|
1
追記修正
test
CHANGED
@@ -1,4 +1,12 @@
|
|
1
|
-
自分なら
|
1
|
+
自分ならCustomPickerView.xibと対応するCustomPickerView.swift、そしてCustomPickerのContainer用の.swiftファイルを作成。CustomPickerView.xibにUIDatePickerとUIToolBar, BarButtonItem載せてBarButtonItemをIBActionで接続、そのBarButtonItemをタップするとUIDatePickerの日付を取得してそれを任意のCellに書き込む、って方法を取りますかね。
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
手前味噌ですが.xibファイルの作り方です。参考程度に。CustomPickerView.xibではFile'sOwnerではなくViewのCustomClassにCustomPickerViewを選択します。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
[https://teratail.com/questions/100469](https://teratail.com/questions/100469)
|
2
10
|
|
3
11
|
|
4
12
|
|