質問編集履歴
1
fuzzball様、すみませんでした。ソースコードを分かるように致しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -36,27 +36,193 @@
|
|
36
36
|
|
37
37
|
|
38
38
|
|
39
|
-
import UIKit
|
39
|
+
```import UIKit
|
40
|
+
|
41
|
+
|
40
42
|
|
41
43
|
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
|
42
44
|
|
45
|
+
|
46
|
+
|
47
|
+
// テーブルビュー
|
48
|
+
|
49
|
+
@IBOutlet var tableView: UITableView!
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
// セルの要素をあらかじめ決めうち
|
54
|
+
|
55
|
+
let someArray: [String] = ["果物", "スポーツ", "アニメ", "野菜", "映画", "飲み物"]
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
// 選択されたセルを覚える変数
|
60
|
+
|
61
|
+
var chosenCell: Int!
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
override func viewDidLoad() {
|
66
|
+
|
67
|
+
super.viewDidLoad()
|
68
|
+
|
69
|
+
// Do any additional setup after loading the view, typically from a nib.
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
// テーブルビューにTableViewCellを登録
|
74
|
+
|
75
|
+
tableView.register(UINib(nibName: "TableViewCell", bundle: nil),forCellReuseIdentifier: "cell_01")
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
tableView.delegate = self
|
80
|
+
|
81
|
+
tableView.dataSource = self
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
override func didReceiveMemoryWarning() {
|
90
|
+
|
91
|
+
super.didReceiveMemoryWarning()
|
92
|
+
|
93
|
+
// Dispose of any resources that can be recreated.
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
// セルの数を指定
|
100
|
+
|
101
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
102
|
+
|
103
|
+
// someArrayの中身の数だけセルを表示
|
104
|
+
|
105
|
+
return someArray.count
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
// 各セルの要素を指定
|
112
|
+
|
113
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
114
|
+
|
115
|
+
// UITableViewCellのインスタンスを生成
|
116
|
+
|
117
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "cell_01", for: indexPath) as! TableViewCell
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
// セルのUILabelに配列の中身を順に表示
|
122
|
+
|
123
|
+
cell.label.text = someArray[indexPath.row]
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
return cell
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
// セルが選択された時に呼ばれる
|
134
|
+
|
135
|
+
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
136
|
+
|
137
|
+
// 選択されたcellの番号を記憶
|
138
|
+
|
139
|
+
chosenCell = indexPath.row
|
140
|
+
|
141
|
+
// 画面遷移の準備
|
142
|
+
|
143
|
+
performSegue(withIdentifier: "toSecondViewController",sender: nil)
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
// Segue 準備
|
150
|
+
|
151
|
+
override func prepare(for segue: UIStoryboardSegue, sender: Any!) {
|
152
|
+
|
153
|
+
// 遷移先のViecControllerのインスタンスを生成
|
154
|
+
|
155
|
+
let secVC: secondViewController = (segue.destination as? secondViewController)!
|
156
|
+
|
157
|
+
// secondViewControllerのgetCellに選択された画像を設定する
|
158
|
+
|
159
|
+
secVC.getCell = chosenCell
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
```
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
2枚目
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
```import UIKit
|
178
|
+
|
179
|
+
class secondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
|
180
|
+
|
43
181
|
|
44
182
|
|
45
183
|
// テーブルビュー
|
46
184
|
|
47
|
-
@IBOutlet var
|
185
|
+
@IBOutlet var secondTableView: UITableView!
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
// ViewControllerから選択されたCell番号を受け取る変数
|
190
|
+
|
191
|
+
var getCell: Int!
|
48
192
|
|
49
193
|
|
50
194
|
|
51
195
|
// セルの要素をあらかじめ決めうち
|
52
196
|
|
197
|
+
// 果物
|
198
|
+
|
53
|
-
let
|
199
|
+
let fruitArray: [String] = ["リンゴ", "ブドウ", "バナナ", "オレンジ", "レモン", "ピーチ"]
|
200
|
+
|
54
|
-
|
201
|
+
// スポーツ
|
202
|
+
|
55
|
-
|
203
|
+
let sportArray: [String] = ["サッカー", "野球", "ラグビー", "卓球"]
|
204
|
+
|
56
|
-
|
205
|
+
// アニメ
|
206
|
+
|
207
|
+
let animeArray: [String] = ["ポ", "プ", "テ", "ピ", "ピ", "ッ", "ク"]
|
208
|
+
|
209
|
+
// 野菜
|
210
|
+
|
211
|
+
let vegetableArray: [String] = ["スイカ", "トマト", "キャベツ", "キュウリ", "大豆", "レタス", "サニーレタス"]
|
212
|
+
|
213
|
+
// 映画
|
214
|
+
|
215
|
+
let movieArray: [String] = ["ハリーポッター", "スターウォーズ", "アイアンマン", "スパイダーマン", "ゴーストバスターズ", "ブラックホークダウン", "タイタニック", "バックトゥーザフューチャー", "ジュラシックパーク"]
|
216
|
+
|
217
|
+
// 飲み物
|
218
|
+
|
219
|
+
let drinkArray: [String] = ["コーラ", "烏龍茶"]
|
220
|
+
|
221
|
+
|
222
|
+
|
57
|
-
// 選択された
|
223
|
+
// 選択されたジャンルの配列の長さ変数
|
58
|
-
|
224
|
+
|
59
|
-
var
|
225
|
+
var arrayLength: Int!
|
60
226
|
|
61
227
|
|
62
228
|
|
@@ -64,19 +230,67 @@
|
|
64
230
|
|
65
231
|
super.viewDidLoad()
|
66
232
|
|
67
|
-
// Do any additional setup after loading the view, typically from a nib.
|
68
|
-
|
69
233
|
|
70
234
|
|
71
235
|
// テーブルビューにTableViewCellを登録
|
72
236
|
|
73
|
-
|
237
|
+
secondTableView.register(UINib(nibName: "secondTableViewCell", bundle: nil),forCellReuseIdentifier: "cell_02")
|
74
|
-
|
75
|
-
|
76
|
-
|
238
|
+
|
239
|
+
|
240
|
+
|
77
|
-
|
241
|
+
secondTableView.delegate = self
|
78
|
-
|
242
|
+
|
79
|
-
|
243
|
+
secondTableView.dataSource = self
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
// 選択されたジャンルによってセルの数を変える
|
248
|
+
|
249
|
+
// getCellの中身が・・・
|
250
|
+
|
251
|
+
switch getCell {
|
252
|
+
|
253
|
+
// 0のとき
|
254
|
+
|
255
|
+
case 0:
|
256
|
+
|
257
|
+
arrayLength = fruitArray.count
|
258
|
+
|
259
|
+
// 1のとき
|
260
|
+
|
261
|
+
case 1:
|
262
|
+
|
263
|
+
arrayLength = sportArray.count
|
264
|
+
|
265
|
+
// 2のとき
|
266
|
+
|
267
|
+
case 2:
|
268
|
+
|
269
|
+
arrayLength = animeArray.count
|
270
|
+
|
271
|
+
// 3のとき
|
272
|
+
|
273
|
+
case 3:
|
274
|
+
|
275
|
+
arrayLength = vegetableArray.count
|
276
|
+
|
277
|
+
// 4のとき
|
278
|
+
|
279
|
+
case 4:
|
280
|
+
|
281
|
+
arrayLength = movieArray.count
|
282
|
+
|
283
|
+
// 5のとき
|
284
|
+
|
285
|
+
case 5:
|
286
|
+
|
287
|
+
arrayLength = drinkArray.count
|
288
|
+
|
289
|
+
default:
|
290
|
+
|
291
|
+
break
|
292
|
+
|
293
|
+
}
|
80
294
|
|
81
295
|
|
82
296
|
|
@@ -94,13 +308,15 @@
|
|
94
308
|
|
95
309
|
|
96
310
|
|
311
|
+
|
312
|
+
|
97
313
|
// セルの数を指定
|
98
314
|
|
99
315
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
100
316
|
|
101
317
|
// someArrayの中身の数だけセルを表示
|
102
318
|
|
103
|
-
return
|
319
|
+
return arrayLength
|
104
320
|
|
105
321
|
}
|
106
322
|
|
@@ -112,13 +328,59 @@
|
|
112
328
|
|
113
329
|
// UITableViewCellのインスタンスを生成
|
114
330
|
|
115
|
-
let cell =
|
331
|
+
let cell = secondTableView.dequeueReusableCell(withIdentifier: "cell_02", for: indexPath) as! secondTableViewCell
|
332
|
+
|
333
|
+
|
334
|
+
|
116
|
-
|
335
|
+
// 選択されたジャンルによってセルの中身を変える
|
336
|
+
|
117
|
-
|
337
|
+
// getCellの中身が・・・
|
338
|
+
|
118
|
-
|
339
|
+
switch getCell {
|
340
|
+
|
341
|
+
// 0のとき
|
342
|
+
|
343
|
+
case 0:
|
344
|
+
|
119
|
-
// セルのUILabelに配列の中身を順に表示
|
345
|
+
// セルのUILabelに配列の中身を順に表示
|
346
|
+
|
120
|
-
|
347
|
+
cell.secondLabel.text = fruitArray[indexPath.row]
|
348
|
+
|
349
|
+
// 1のとき
|
350
|
+
|
351
|
+
case 1:
|
352
|
+
|
121
|
-
cell.
|
353
|
+
cell.secondLabel.text = sportArray[indexPath.row]
|
354
|
+
|
355
|
+
// 2のとき
|
356
|
+
|
357
|
+
case 2:
|
358
|
+
|
359
|
+
cell.secondLabel.text = animeArray[indexPath.row]
|
360
|
+
|
361
|
+
// 3のとき
|
362
|
+
|
363
|
+
case 3:
|
364
|
+
|
365
|
+
cell.secondLabel.text = vegetableArray[indexPath.row]
|
366
|
+
|
367
|
+
// 4のとき
|
368
|
+
|
369
|
+
case 4:
|
370
|
+
|
371
|
+
cell.secondLabel.text = movieArray[indexPath.row]
|
372
|
+
|
373
|
+
// 5のとき
|
374
|
+
|
375
|
+
case 5:
|
376
|
+
|
377
|
+
cell.secondLabel.text = drinkArray[indexPath.row]
|
378
|
+
|
379
|
+
default:
|
380
|
+
|
381
|
+
break
|
382
|
+
|
383
|
+
}
|
122
384
|
|
123
385
|
|
124
386
|
|
@@ -126,260 +388,6 @@
|
|
126
388
|
|
127
389
|
}
|
128
390
|
|
129
|
-
|
130
|
-
|
131
|
-
// セルが選択された時に呼ばれる
|
132
|
-
|
133
|
-
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
134
|
-
|
135
|
-
// 選択されたcellの番号を記憶
|
136
|
-
|
137
|
-
chosenCell = indexPath.row
|
138
|
-
|
139
|
-
// 画面遷移の準備
|
140
|
-
|
141
|
-
performSegue(withIdentifier: "toSecondViewController",sender: nil)
|
142
|
-
|
143
|
-
}
|
391
|
+
}
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
392
|
+
|
148
|
-
|
149
|
-
override func prepare(for segue: UIStoryboardSegue, sender: Any!) {
|
150
|
-
|
151
|
-
// 遷移先のViecControllerのインスタンスを生成
|
152
|
-
|
153
|
-
let secVC: secondViewController = (segue.destination as? secondViewController)!
|
154
|
-
|
155
|
-
// secondViewControllerのgetCellに選択された画像を設定する
|
156
|
-
|
157
|
-
secVC.getCell = chosenCell
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
}
|
162
|
-
|
163
|
-
}
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
393
|
+
```
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
import UIKit
|
172
|
-
|
173
|
-
class secondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
// テーブルビュー
|
178
|
-
|
179
|
-
@IBOutlet var secondTableView: UITableView!
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
// ViewControllerから選択されたCell番号を受け取る変数
|
184
|
-
|
185
|
-
var getCell: Int!
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
// セルの要素をあらかじめ決めうち
|
190
|
-
|
191
|
-
// 果物
|
192
|
-
|
193
|
-
let fruitArray: [String] = ["リンゴ", "ブドウ", "バナナ", "オレンジ", "レモン", "ピーチ"]
|
194
|
-
|
195
|
-
// スポーツ
|
196
|
-
|
197
|
-
let sportArray: [String] = ["サッカー", "野球", "ラグビー", "卓球"]
|
198
|
-
|
199
|
-
// アニメ
|
200
|
-
|
201
|
-
let animeArray: [String] = ["ポ", "プ", "テ", "ピ", "ピ", "ッ", "ク"]
|
202
|
-
|
203
|
-
// 野菜
|
204
|
-
|
205
|
-
let vegetableArray: [String] = ["スイカ", "トマト", "キャベツ", "キュウリ", "大豆", "レタス", "サニーレタス"]
|
206
|
-
|
207
|
-
// 映画
|
208
|
-
|
209
|
-
let movieArray: [String] = ["ハリーポッター", "スターウォーズ", "アイアンマン", "スパイダーマン", "ゴーストバスターズ", "ブラックホークダウン", "タイタニック", "バックトゥーザフューチャー", "ジュラシックパーク"]
|
210
|
-
|
211
|
-
// 飲み物
|
212
|
-
|
213
|
-
let drinkArray: [String] = ["コーラ", "烏龍茶"]
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
// 選択されたジャンルの配列の長さ変数
|
218
|
-
|
219
|
-
var arrayLength: Int!
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
override func viewDidLoad() {
|
224
|
-
|
225
|
-
super.viewDidLoad()
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
// テーブルビューにTableViewCellを登録
|
230
|
-
|
231
|
-
secondTableView.register(UINib(nibName: "secondTableViewCell", bundle: nil),forCellReuseIdentifier: "cell_02")
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
secondTableView.delegate = self
|
236
|
-
|
237
|
-
secondTableView.dataSource = self
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
// 選択されたジャンルによってセルの数を変える
|
242
|
-
|
243
|
-
// getCellの中身が・・・
|
244
|
-
|
245
|
-
switch getCell {
|
246
|
-
|
247
|
-
// 0のとき
|
248
|
-
|
249
|
-
case 0:
|
250
|
-
|
251
|
-
arrayLength = fruitArray.count
|
252
|
-
|
253
|
-
// 1のとき
|
254
|
-
|
255
|
-
case 1:
|
256
|
-
|
257
|
-
arrayLength = sportArray.count
|
258
|
-
|
259
|
-
// 2のとき
|
260
|
-
|
261
|
-
case 2:
|
262
|
-
|
263
|
-
arrayLength = animeArray.count
|
264
|
-
|
265
|
-
// 3のとき
|
266
|
-
|
267
|
-
case 3:
|
268
|
-
|
269
|
-
arrayLength = vegetableArray.count
|
270
|
-
|
271
|
-
// 4のとき
|
272
|
-
|
273
|
-
case 4:
|
274
|
-
|
275
|
-
arrayLength = movieArray.count
|
276
|
-
|
277
|
-
// 5のとき
|
278
|
-
|
279
|
-
case 5:
|
280
|
-
|
281
|
-
arrayLength = drinkArray.count
|
282
|
-
|
283
|
-
default:
|
284
|
-
|
285
|
-
break
|
286
|
-
|
287
|
-
}
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
}
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
override func didReceiveMemoryWarning() {
|
296
|
-
|
297
|
-
super.didReceiveMemoryWarning()
|
298
|
-
|
299
|
-
// Dispose of any resources that can be recreated.
|
300
|
-
|
301
|
-
}
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
// セルの数を指定
|
308
|
-
|
309
|
-
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
310
|
-
|
311
|
-
// someArrayの中身の数だけセルを表示
|
312
|
-
|
313
|
-
return arrayLength
|
314
|
-
|
315
|
-
}
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
// 各セルの要素を指定
|
320
|
-
|
321
|
-
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
322
|
-
|
323
|
-
// UITableViewCellのインスタンスを生成
|
324
|
-
|
325
|
-
let cell = secondTableView.dequeueReusableCell(withIdentifier: "cell_02", for: indexPath) as! secondTableViewCell
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
// 選択されたジャンルによってセルの中身を変える
|
330
|
-
|
331
|
-
// getCellの中身が・・・
|
332
|
-
|
333
|
-
switch getCell {
|
334
|
-
|
335
|
-
// 0のとき
|
336
|
-
|
337
|
-
case 0:
|
338
|
-
|
339
|
-
// セルのUILabelに配列の中身を順に表示
|
340
|
-
|
341
|
-
cell.secondLabel.text = fruitArray[indexPath.row]
|
342
|
-
|
343
|
-
// 1のとき
|
344
|
-
|
345
|
-
case 1:
|
346
|
-
|
347
|
-
cell.secondLabel.text = sportArray[indexPath.row]
|
348
|
-
|
349
|
-
// 2のとき
|
350
|
-
|
351
|
-
case 2:
|
352
|
-
|
353
|
-
cell.secondLabel.text = animeArray[indexPath.row]
|
354
|
-
|
355
|
-
// 3のとき
|
356
|
-
|
357
|
-
case 3:
|
358
|
-
|
359
|
-
cell.secondLabel.text = vegetableArray[indexPath.row]
|
360
|
-
|
361
|
-
// 4のとき
|
362
|
-
|
363
|
-
case 4:
|
364
|
-
|
365
|
-
cell.secondLabel.text = movieArray[indexPath.row]
|
366
|
-
|
367
|
-
// 5のとき
|
368
|
-
|
369
|
-
case 5:
|
370
|
-
|
371
|
-
cell.secondLabel.text = drinkArray[indexPath.row]
|
372
|
-
|
373
|
-
default:
|
374
|
-
|
375
|
-
break
|
376
|
-
|
377
|
-
}
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
return cell
|
382
|
-
|
383
|
-
}
|
384
|
-
|
385
|
-
}
|