質問編集履歴
5
内容修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,16 +14,54 @@
|
|
14
14
|
|
15
15
|
|
16
16
|
|
17
|
+
目指している形
|
18
|
+
|
19
|
+
https://medium.com/@smehta/ios-swift-creating-a-dynamic-picker-view-843b3290e7f0
|
20
|
+
|
21
|
+
|
22
|
+
|
17
23
|
初めての質問なので足りない部分があったらすみません。
|
18
24
|
|
19
25
|
|
20
26
|
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
### **4/3 23:11 結果的に質問したい事がわからないのでアドバイスができないという指摘を受けてコードを修正+わからない事を箇条書きにしました。
|
34
|
+
|
35
|
+
**
|
36
|
+
|
37
|
+
わからない部分
|
38
|
+
|
39
|
+
①コンポーネント番号取得まではできたが、そこからその番号を用いて分岐させたいが、どう動的に変化させるかがわからない
|
40
|
+
|
41
|
+
②表示を司る部分
|
42
|
+
|
43
|
+
func pickerView(_ pickerView: UIPickerView,
|
44
|
+
|
45
|
+
titleForRow row: Int,
|
46
|
+
|
47
|
+
forComponent component: Int) -> String? {}
|
48
|
+
|
49
|
+
この関数内で現在選ばれているコンポーネント番号を取得(comNum = pickerView.selectedRow(inComponent: 0))して分岐させようとしたが、選択されている番号と違った数字が表示され上手くいかないので結果的に動的に変化させる事ができない。
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
わかりづらい説明ですみませんが分かる方いらしたらよろしくお願いします。
|
54
|
+
|
55
|
+
|
56
|
+
|
21
57
|
### 現在のコード
|
22
58
|
|
23
59
|
|
24
60
|
|
25
61
|
```swift
|
26
62
|
|
63
|
+
|
64
|
+
|
27
65
|
import UIKit
|
28
66
|
|
29
67
|
|
@@ -42,15 +80,15 @@
|
|
42
80
|
|
43
81
|
|
44
82
|
|
45
|
-
let todou = [
|
83
|
+
let todou = [ "東京都", "高知県", "香川県", "愛媛県"]
|
46
84
|
|
47
85
|
|
48
86
|
|
49
87
|
let komakai = ["江戸川区", "江東区", "中央区", "荒川区"]
|
50
88
|
|
51
|
-
|
52
|
-
|
89
|
+
|
90
|
+
|
53
|
-
let komakai2 = ["高知市", "四万十市", "安芸市"]
|
91
|
+
let komakai2 = ["高知市", "四万十市", "安芸市", "なんとか市"]
|
54
92
|
|
55
93
|
|
56
94
|
|
@@ -58,6 +96,8 @@
|
|
58
96
|
|
59
97
|
|
60
98
|
|
99
|
+
|
100
|
+
|
61
101
|
override func viewDidLoad() {
|
62
102
|
|
63
103
|
super.viewDidLoad()
|
@@ -144,13 +184,11 @@
|
|
144
184
|
|
145
185
|
if (component == 0){
|
146
186
|
|
147
|
-
print("component0")
|
148
|
-
|
149
187
|
return todou.count
|
150
188
|
|
189
|
+
}
|
190
|
+
|
151
|
-
|
191
|
+
else if (component == 1){
|
152
|
-
|
153
|
-
print("component1")
|
154
192
|
|
155
193
|
return komakai.count
|
156
194
|
|
@@ -160,8 +198,6 @@
|
|
160
198
|
|
161
199
|
}
|
162
200
|
|
163
|
-
|
164
|
-
|
165
201
|
// UIPickerViewの最初の表示
|
166
202
|
|
167
203
|
//その都度呼び出し
|
@@ -178,20 +214,12 @@
|
|
178
214
|
|
179
215
|
}
|
180
216
|
|
181
|
-
else if (component == 1)
|
217
|
+
else if (component == 1){
|
182
218
|
|
183
219
|
return komakai[row]
|
184
220
|
|
185
221
|
}
|
186
222
|
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
223
|
return "";
|
196
224
|
|
197
225
|
|
@@ -202,42 +230,36 @@
|
|
202
230
|
|
203
231
|
// UIPickerViewのRowが選択された時の挙動
|
204
232
|
|
233
|
+
func pickerView(_ pickerView: UIPickerView,didSelectRow row: Int, inComponent component: Int) {
|
234
|
+
|
235
|
+
var comNum = 0
|
236
|
+
|
237
|
+
//現在選択されているコンポーネント番号取得
|
238
|
+
|
205
|
-
|
239
|
+
comNum = pickerView.selectedRow(inComponent: 0)
|
206
|
-
|
207
|
-
didSelectRow row: Int,
|
208
|
-
|
209
|
-
inComponent component: Int) {
|
210
|
-
|
211
|
-
// print("Rowが選択されました")
|
212
240
|
|
213
241
|
label.text = todou[row]
|
214
242
|
|
243
|
+
switch comNum {
|
244
|
+
|
245
|
+
case 1:
|
246
|
+
|
247
|
+
print("更新")
|
248
|
+
|
249
|
+
pickerView.reloadComponent(1)
|
250
|
+
|
251
|
+
default:
|
252
|
+
|
253
|
+
print("default")
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
|
258
|
+
|
215
259
|
}
|
216
260
|
|
217
261
|
|
218
262
|
|
219
263
|
}
|
220
264
|
|
221
|
-
|
222
|
-
|
223
265
|
```
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
どの都道府県が入力されているか取得して、それをswitch文を用いて判別し、それに対応した配列を用意して表示するといった内容にしようとしています。
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
なのでcompornent0(一列目)のどれにカーソルがあたっているのかが分かる方法があれば知りたいです。
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
### 試したこと
|
236
|
-
|
237
|
-
質問掲示板を色々と回って見たが、Pickerviewを二つ用いて実装しているものしかありませんでした。
|
238
|
-
|
239
|
-
Pickerviewを二つ用いればできたが一つのPickerviewでの実装ができませんでした、一つので無理な様だったら諦めます。
|
240
|
-
|
241
|
-
目指しているものに一番近い実装が掲示板にありました。(二つのPickerviewを使っている)
|
242
|
-
|
243
|
-
https://teratail.com/questions/121810
|
4
追加修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -228,6 +228,8 @@
|
|
228
228
|
|
229
229
|
|
230
230
|
|
231
|
+
なのでcompornent0(一列目)のどれにカーソルがあたっているのかが分かる方法があれば知りたいです。
|
232
|
+
|
231
233
|
|
232
234
|
|
233
235
|
### 試したこと
|
3
内容の順番がわかりづらかったので並び替え修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -18,6 +18,216 @@
|
|
18
18
|
|
19
19
|
|
20
20
|
|
21
|
+
### 現在のコード
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
```swift
|
26
|
+
|
27
|
+
import UIKit
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
class ViewController: UIViewController , UIPickerViewDelegate, UIPickerViewDataSource {
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
var pickerView = UIPickerView()
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
var label = UILabel()
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
let todou = ["", "東京都", "高知県", "香川県", "愛媛県"]
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
let komakai = ["江戸川区", "江東区", "中央区", "荒川区"]
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
let komakai2 = ["高知市", "四万十市", "安芸市"]
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
let komakai3 = ["松山市"]
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
override func viewDidLoad() {
|
62
|
+
|
63
|
+
super.viewDidLoad()
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
// ViewContorller 背景色
|
68
|
+
|
69
|
+
self.view.backgroundColor = UIColor(red: 0.92, green: 1.0, blue: 0.94, alpha: 1.0)
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
// PickerView のサイズと位置
|
74
|
+
|
75
|
+
pickerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 300)
|
76
|
+
|
77
|
+
pickerView.backgroundColor = UIColor(red: 0.69, green: 0.93, blue: 0.9, alpha: 1.0)
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
// PickerViewはスクリーンの中央に設定
|
82
|
+
|
83
|
+
pickerView.center = self.view.center
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
// Delegate設定
|
88
|
+
|
89
|
+
pickerView.delegate = self
|
90
|
+
|
91
|
+
pickerView.dataSource = self
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
// Label 位置はPickerViewより150上に設定、高さを2行表示分とる
|
96
|
+
|
97
|
+
let yPosition = pickerView.frame.origin.y
|
98
|
+
|
99
|
+
label.frame = CGRect(x: 0, y: yPosition - 100, width: self.view.frame.width, height: 100)
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
// ラベルを2行表示
|
104
|
+
|
105
|
+
label.numberOfLines = 2
|
106
|
+
|
107
|
+
label.text = "都道府県"
|
108
|
+
|
109
|
+
// フォントサイズを大きく
|
110
|
+
|
111
|
+
label.font = UIFont.systemFont(ofSize: 36)
|
112
|
+
|
113
|
+
// テキストを中央寄せにする
|
114
|
+
|
115
|
+
label.textAlignment = NSTextAlignment.center
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
self.view.addSubview(pickerView)
|
120
|
+
|
121
|
+
self.view.addSubview(label)
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
// UIPickerViewのコンポーネント(列)の数
|
128
|
+
|
129
|
+
func numberOfComponents(in pickerView: UIPickerView) -> Int {
|
130
|
+
|
131
|
+
return 2
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
// UIPickerViewの行数、リストの数
|
138
|
+
|
139
|
+
//配列の数をカウントして表示する、必須
|
140
|
+
|
141
|
+
func pickerView(_ pickerView: UIPickerView,
|
142
|
+
|
143
|
+
numberOfRowsInComponent component: Int) -> Int {
|
144
|
+
|
145
|
+
if (component == 0){
|
146
|
+
|
147
|
+
print("component0")
|
148
|
+
|
149
|
+
return todou.count
|
150
|
+
|
151
|
+
}else if (component == 1){
|
152
|
+
|
153
|
+
print("component1")
|
154
|
+
|
155
|
+
return komakai.count
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
return 0;
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
// UIPickerViewの最初の表示
|
166
|
+
|
167
|
+
//その都度呼び出し
|
168
|
+
|
169
|
+
func pickerView(_ pickerView: UIPickerView,
|
170
|
+
|
171
|
+
titleForRow row: Int,
|
172
|
+
|
173
|
+
forComponent component: Int) -> String? {
|
174
|
+
|
175
|
+
if (component == 0){
|
176
|
+
|
177
|
+
return todou[row]
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
else if (component == 1) {
|
182
|
+
|
183
|
+
return komakai[row]
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
return "";
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
// UIPickerViewのRowが選択された時の挙動
|
204
|
+
|
205
|
+
func pickerView(_ pickerView: UIPickerView,
|
206
|
+
|
207
|
+
didSelectRow row: Int,
|
208
|
+
|
209
|
+
inComponent component: Int) {
|
210
|
+
|
211
|
+
// print("Rowが選択されました")
|
212
|
+
|
213
|
+
label.text = todou[row]
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
```
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
どの都道府県が入力されているか取得して、それをswitch文を用いて判別し、それに対応した配列を用意して表示するといった内容にしようとしています。
|
228
|
+
|
229
|
+
|
230
|
+
|
21
231
|
|
22
232
|
|
23
233
|
### 試したこと
|
@@ -29,209 +239,3 @@
|
|
29
239
|
目指しているものに一番近い実装が掲示板にありました。(二つのPickerviewを使っている)
|
30
240
|
|
31
241
|
https://teratail.com/questions/121810
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
```swift
|
36
|
-
|
37
|
-
import UIKit
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
class ViewController: UIViewController , UIPickerViewDelegate, UIPickerViewDataSource {
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
var pickerView = UIPickerView()
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
var label = UILabel()
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
let todou = ["", "東京都", "高知県", "香川県", "愛媛県"]
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
let komakai = ["江戸川区", "江東区", "中央区", "荒川区"]
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
let komakai2 = ["高知市", "四万十市", "安芸市"]
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
let komakai3 = ["松山市"]
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
override func viewDidLoad() {
|
72
|
-
|
73
|
-
super.viewDidLoad()
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
// ViewContorller 背景色
|
78
|
-
|
79
|
-
self.view.backgroundColor = UIColor(red: 0.92, green: 1.0, blue: 0.94, alpha: 1.0)
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
// PickerView のサイズと位置
|
84
|
-
|
85
|
-
pickerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 300)
|
86
|
-
|
87
|
-
pickerView.backgroundColor = UIColor(red: 0.69, green: 0.93, blue: 0.9, alpha: 1.0)
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
// PickerViewはスクリーンの中央に設定
|
92
|
-
|
93
|
-
pickerView.center = self.view.center
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
// Delegate設定
|
98
|
-
|
99
|
-
pickerView.delegate = self
|
100
|
-
|
101
|
-
pickerView.dataSource = self
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
// Label 位置はPickerViewより150上に設定、高さを2行表示分とる
|
106
|
-
|
107
|
-
let yPosition = pickerView.frame.origin.y
|
108
|
-
|
109
|
-
label.frame = CGRect(x: 0, y: yPosition - 100, width: self.view.frame.width, height: 100)
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
// ラベルを2行表示
|
114
|
-
|
115
|
-
label.numberOfLines = 2
|
116
|
-
|
117
|
-
label.text = "都道府県"
|
118
|
-
|
119
|
-
// フォントサイズを大きく
|
120
|
-
|
121
|
-
label.font = UIFont.systemFont(ofSize: 36)
|
122
|
-
|
123
|
-
// テキストを中央寄せにする
|
124
|
-
|
125
|
-
label.textAlignment = NSTextAlignment.center
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
self.view.addSubview(pickerView)
|
130
|
-
|
131
|
-
self.view.addSubview(label)
|
132
|
-
|
133
|
-
}
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
// UIPickerViewのコンポーネント(列)の数
|
138
|
-
|
139
|
-
func numberOfComponents(in pickerView: UIPickerView) -> Int {
|
140
|
-
|
141
|
-
return 2
|
142
|
-
|
143
|
-
}
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
// UIPickerViewの行数、リストの数
|
148
|
-
|
149
|
-
//配列の数をカウントして表示する、必須
|
150
|
-
|
151
|
-
func pickerView(_ pickerView: UIPickerView,
|
152
|
-
|
153
|
-
numberOfRowsInComponent component: Int) -> Int {
|
154
|
-
|
155
|
-
if (component == 0){
|
156
|
-
|
157
|
-
print("component0")
|
158
|
-
|
159
|
-
return todou.count
|
160
|
-
|
161
|
-
}else if (component == 1){
|
162
|
-
|
163
|
-
print("component1")
|
164
|
-
|
165
|
-
return komakai.count
|
166
|
-
|
167
|
-
}
|
168
|
-
|
169
|
-
return 0;
|
170
|
-
|
171
|
-
}
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
// UIPickerViewの最初の表示
|
176
|
-
|
177
|
-
//その都度呼び出し
|
178
|
-
|
179
|
-
func pickerView(_ pickerView: UIPickerView,
|
180
|
-
|
181
|
-
titleForRow row: Int,
|
182
|
-
|
183
|
-
forComponent component: Int) -> String? {
|
184
|
-
|
185
|
-
if (component == 0){
|
186
|
-
|
187
|
-
return todou[row]
|
188
|
-
|
189
|
-
}
|
190
|
-
|
191
|
-
else if (component == 1) {
|
192
|
-
|
193
|
-
return komakai[row]
|
194
|
-
|
195
|
-
}
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
return "";
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
}
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
// UIPickerViewのRowが選択された時の挙動
|
214
|
-
|
215
|
-
func pickerView(_ pickerView: UIPickerView,
|
216
|
-
|
217
|
-
didSelectRow row: Int,
|
218
|
-
|
219
|
-
inComponent component: Int) {
|
220
|
-
|
221
|
-
// print("Rowが選択されました")
|
222
|
-
|
223
|
-
label.text = todou[row]
|
224
|
-
|
225
|
-
}
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
}
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
```
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
どの都道府県が入力されているか取得して、それをswitch文を用いて判別し、それに対応した配列を用意して表示するといった内容にしようとしています。
|
2
コードを追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -29,3 +29,209 @@
|
|
29
29
|
目指しているものに一番近い実装が掲示板にありました。(二つのPickerviewを使っている)
|
30
30
|
|
31
31
|
https://teratail.com/questions/121810
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
```swift
|
36
|
+
|
37
|
+
import UIKit
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
class ViewController: UIViewController , UIPickerViewDelegate, UIPickerViewDataSource {
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
var pickerView = UIPickerView()
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
var label = UILabel()
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
let todou = ["", "東京都", "高知県", "香川県", "愛媛県"]
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
let komakai = ["江戸川区", "江東区", "中央区", "荒川区"]
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
let komakai2 = ["高知市", "四万十市", "安芸市"]
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
let komakai3 = ["松山市"]
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
override func viewDidLoad() {
|
72
|
+
|
73
|
+
super.viewDidLoad()
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
// ViewContorller 背景色
|
78
|
+
|
79
|
+
self.view.backgroundColor = UIColor(red: 0.92, green: 1.0, blue: 0.94, alpha: 1.0)
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
// PickerView のサイズと位置
|
84
|
+
|
85
|
+
pickerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 300)
|
86
|
+
|
87
|
+
pickerView.backgroundColor = UIColor(red: 0.69, green: 0.93, blue: 0.9, alpha: 1.0)
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
// PickerViewはスクリーンの中央に設定
|
92
|
+
|
93
|
+
pickerView.center = self.view.center
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
// Delegate設定
|
98
|
+
|
99
|
+
pickerView.delegate = self
|
100
|
+
|
101
|
+
pickerView.dataSource = self
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
// Label 位置はPickerViewより150上に設定、高さを2行表示分とる
|
106
|
+
|
107
|
+
let yPosition = pickerView.frame.origin.y
|
108
|
+
|
109
|
+
label.frame = CGRect(x: 0, y: yPosition - 100, width: self.view.frame.width, height: 100)
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
// ラベルを2行表示
|
114
|
+
|
115
|
+
label.numberOfLines = 2
|
116
|
+
|
117
|
+
label.text = "都道府県"
|
118
|
+
|
119
|
+
// フォントサイズを大きく
|
120
|
+
|
121
|
+
label.font = UIFont.systemFont(ofSize: 36)
|
122
|
+
|
123
|
+
// テキストを中央寄せにする
|
124
|
+
|
125
|
+
label.textAlignment = NSTextAlignment.center
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
self.view.addSubview(pickerView)
|
130
|
+
|
131
|
+
self.view.addSubview(label)
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
// UIPickerViewのコンポーネント(列)の数
|
138
|
+
|
139
|
+
func numberOfComponents(in pickerView: UIPickerView) -> Int {
|
140
|
+
|
141
|
+
return 2
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
// UIPickerViewの行数、リストの数
|
148
|
+
|
149
|
+
//配列の数をカウントして表示する、必須
|
150
|
+
|
151
|
+
func pickerView(_ pickerView: UIPickerView,
|
152
|
+
|
153
|
+
numberOfRowsInComponent component: Int) -> Int {
|
154
|
+
|
155
|
+
if (component == 0){
|
156
|
+
|
157
|
+
print("component0")
|
158
|
+
|
159
|
+
return todou.count
|
160
|
+
|
161
|
+
}else if (component == 1){
|
162
|
+
|
163
|
+
print("component1")
|
164
|
+
|
165
|
+
return komakai.count
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
return 0;
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
// UIPickerViewの最初の表示
|
176
|
+
|
177
|
+
//その都度呼び出し
|
178
|
+
|
179
|
+
func pickerView(_ pickerView: UIPickerView,
|
180
|
+
|
181
|
+
titleForRow row: Int,
|
182
|
+
|
183
|
+
forComponent component: Int) -> String? {
|
184
|
+
|
185
|
+
if (component == 0){
|
186
|
+
|
187
|
+
return todou[row]
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
else if (component == 1) {
|
192
|
+
|
193
|
+
return komakai[row]
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
return "";
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
// UIPickerViewのRowが選択された時の挙動
|
214
|
+
|
215
|
+
func pickerView(_ pickerView: UIPickerView,
|
216
|
+
|
217
|
+
didSelectRow row: Int,
|
218
|
+
|
219
|
+
inComponent component: Int) {
|
220
|
+
|
221
|
+
// print("Rowが選択されました")
|
222
|
+
|
223
|
+
label.text = todou[row]
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
```
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
どの都道府県が入力されているか取得して、それをswitch文を用いて判別し、それに対応した配列を用意して表示するといった内容にしようとしています。
|
1
スペルミス
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
swift、Pickerviewで一つめのcompornentの選択内容に応じて二つの目のcompornet表示を切り替える方法
|
1
|
+
swift、Pickerviewで一つめのcompornentの選択内容に応じて二つの目のcompornent表示を切り替える方法
|
test
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
-
Pickerviewのドラムロールのcompornet0で大まかな場所(都道府県)、それを選択するとcompornet1に、都道府県に対応した市区町村が出てきて選択可能状態になる。
|
9
|
+
Pickerviewのドラムロールのcompornent0で大まかな場所(都道府県)、それを選択するとcompornent1に、都道府県に対応した市区町村が出てきて選択可能状態になる。
|
10
10
|
|
11
11
|
|
12
12
|
|