質問編集履歴
3
追加修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -293,3 +293,143 @@
|
|
293
293
|
[019 セクション分けしたUITableViewを作る](https://sites.google.com/a/gclue.jp/swift-docs/ni-yinki100-ios/uikit/sekushon-fenkeshitauitableviewwo-zuoru)
|
294
294
|
|
295
295
|
[[swift]ネストしたDictionaryの取得](https://qiita.com/kohei1218/items/a68deeff3add4b2a2d6e)
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
### 追加修正
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
```swift
|
308
|
+
|
309
|
+
import UIKit
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
struct HogeItem: Decodable {
|
314
|
+
|
315
|
+
let Name: String
|
316
|
+
|
317
|
+
let Image: String
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
struct HogeSection: Decodable {
|
324
|
+
|
325
|
+
let SectionName: String
|
326
|
+
|
327
|
+
let items: [HogeItem]
|
328
|
+
|
329
|
+
}
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
var _items: [HogeSection] = []
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
override func viewDidLoad() {
|
342
|
+
|
343
|
+
super.viewDidLoad()
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
if let path = Bundle.main.path(forResource: "DisneyCharacterList", ofType:"plist" ) {
|
348
|
+
|
349
|
+
let data = try! Data(contentsOf: URL(fileURLWithPath: path))
|
350
|
+
|
351
|
+
_items = try! PropertyListDecoder().decode([HogeSection].self, from: data)
|
352
|
+
|
353
|
+
}
|
354
|
+
|
355
|
+
}
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
// 設定
|
360
|
+
|
361
|
+
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
|
362
|
+
|
363
|
+
return _items.count
|
364
|
+
|
365
|
+
}
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
// 設定(行数)
|
370
|
+
|
371
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
372
|
+
|
373
|
+
return _items[section].items.count
|
374
|
+
|
375
|
+
}
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
// 設定(セクションタイトル)
|
380
|
+
|
381
|
+
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
382
|
+
|
383
|
+
return _items[section].SectionName
|
384
|
+
|
385
|
+
}
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
// 設定(セル)
|
390
|
+
|
391
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
392
|
+
|
393
|
+
let item = _items[indexPath.section].items[indexPath.row]
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "DisneyCell", for: indexPath as IndexPath)
|
398
|
+
|
399
|
+
cell.textLabel?.text = item.Name
|
400
|
+
|
401
|
+
cell.imageView?.image = UIImage(named: item.Image)
|
402
|
+
|
403
|
+
return cell
|
404
|
+
|
405
|
+
}
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
//セルタップ時にセルの内容を取得する
|
410
|
+
|
411
|
+
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
412
|
+
|
413
|
+
let item = _items[indexPath.section].items[indexPath.row]
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
print(item.Name)
|
418
|
+
|
419
|
+
}
|
420
|
+
|
421
|
+
}
|
422
|
+
|
423
|
+
```
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
「キーに関連付けられた値はありません」のようなメッセージです。
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
```error
|
432
|
+
|
433
|
+
Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "items", intValue: nil), Swift.DecodingError.Context(codingPath: [_PlistKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: \"items\", intValue: nil) (\"items\").", underlyingError: nil))
|
434
|
+
|
435
|
+
```
|
2
参考サイト追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -290,4 +290,6 @@
|
|
290
290
|
|
291
291
|
[【Objective-C】【Swift】TableViewの基本設定について](https://qiita.com/eito_2/items/374c440ae8b3e3c93178)
|
292
292
|
|
293
|
+
[019 セクション分けしたUITableViewを作る](https://sites.google.com/a/gclue.jp/swift-docs/ni-yinki100-ios/uikit/sekushon-fenkeshitauitableviewwo-zuoru)
|
294
|
+
|
293
295
|
[[swift]ネストしたDictionaryの取得](https://qiita.com/kohei1218/items/a68deeff3add4b2a2d6e)
|
1
内容修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,32 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
-
|
3
|
+
info.plist内のセクション名をUITableVIewに設定する上で必要なのは以下の2つだと思っています。
|
4
|
+
|
5
|
+
・「titleForHeaderInSection」メソッドでセクション名を取得して設定する
|
6
|
+
|
7
|
+
・「cellForRowAt」メソッドでセクション毎のセルを取得して設定する
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
![イメージ説明](7746cbca09a76c1ff8f7c374534a98e9.png)
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
理想は以下の形でTableViewが表示されること
|
16
|
+
|
17
|
+
■Section1
|
18
|
+
|
19
|
+
・Mickey(「Image」「Name」)
|
20
|
+
|
21
|
+
■Section2
|
22
|
+
|
23
|
+
・Minnie(「Image」「Name」)
|
24
|
+
|
25
|
+
・Donald(「Image」「Name」)
|
26
|
+
|
27
|
+
・Goofy(「Image」「Name」)
|
28
|
+
|
29
|
+
・Pluto(「Image」「Name」
|
4
30
|
|
5
31
|
|
6
32
|
|
@@ -8,260 +34,260 @@
|
|
8
34
|
|
9
35
|
|
10
36
|
|
11
|
-
|
37
|
+
現状Arrayの中身を表示しているなので、画像の通りになっています。
|
38
|
+
|
39
|
+
![イメージ説明](32fa82736610427addb929cd695417b5.png)
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
### 該当のソースコード
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
```swift
|
52
|
+
|
53
|
+
import UIKit
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
class ViewController: UIViewController ,UITableViewDelegate, UITableViewDataSource{
|
58
|
+
|
59
|
+
var _items:NSArray = []
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
override func viewDidLoad() {
|
64
|
+
|
65
|
+
super.viewDidLoad()
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
// Plistファイルパス
|
70
|
+
|
71
|
+
let path = Bundle.main.path(forResource: "DisneyCharacterList", ofType:"plist")
|
72
|
+
|
73
|
+
_items = NSArray(contentsOfFile:path!)!
|
74
|
+
|
75
|
+
//println(_items);
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
// 設定
|
82
|
+
|
83
|
+
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
|
84
|
+
|
85
|
+
return 1
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
// 設定(行数)
|
92
|
+
|
93
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int)
|
94
|
+
|
95
|
+
-> Int {
|
96
|
+
|
97
|
+
return _items.count
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
// 設定(セル)
|
104
|
+
|
105
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "DisneyCell", for: indexPath as IndexPath)
|
110
|
+
|
111
|
+
let dic = _items.object(at: indexPath.row) as! NSDictionary
|
112
|
+
|
113
|
+
cell.textLabel!.text = dic.value(forKey: "Name") as? String
|
114
|
+
|
115
|
+
let imageName = dic.value(forKey: "Image") as! String
|
116
|
+
|
117
|
+
cell.imageView?.image = UIImage(named: imageName)
|
118
|
+
|
119
|
+
return cell
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
//セクション数の設定
|
126
|
+
|
127
|
+
func numberOfSections(in tableView: UITableView) -> Int {
|
128
|
+
|
129
|
+
return 2
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
//セクション名設定
|
136
|
+
|
137
|
+
let sectionTitles = ["セクション1","セクション2"]
|
138
|
+
|
139
|
+
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
|
140
|
+
|
141
|
+
{
|
142
|
+
|
143
|
+
return sectionTitles[section]
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
}
|
12
150
|
|
13
151
|
```
|
14
152
|
|
153
|
+
|
154
|
+
|
155
|
+
AssetCatalog
|
156
|
+
|
157
|
+
![イメージ説明](0206d53867295e0fe18902dacf064ec0.png)
|
158
|
+
|
159
|
+
|
160
|
+
|
15
|
-
|
161
|
+
info.Plist
|
162
|
+
|
163
|
+
![イメージ説明](7746cbca09a76c1ff8f7c374534a98e9.png)
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
```DisneyCharacterListPlist
|
168
|
+
|
169
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
170
|
+
|
171
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
172
|
+
|
173
|
+
<plist version="1.0">
|
174
|
+
|
175
|
+
<array>
|
176
|
+
|
177
|
+
<dict>
|
178
|
+
|
179
|
+
<key>SectionName</key>
|
180
|
+
|
181
|
+
<string>Section1</string>
|
182
|
+
|
183
|
+
<key>Items</key>
|
184
|
+
|
185
|
+
<array>
|
186
|
+
|
187
|
+
<dict>
|
188
|
+
|
189
|
+
<key>Image</key>
|
190
|
+
|
191
|
+
<string>0.MickeyIcon</string>
|
192
|
+
|
193
|
+
<key>Name</key>
|
194
|
+
|
195
|
+
<string>Mickey</string>
|
196
|
+
|
197
|
+
</dict>
|
198
|
+
|
199
|
+
</array>
|
200
|
+
|
201
|
+
</dict>
|
202
|
+
|
203
|
+
<dict>
|
204
|
+
|
205
|
+
<key>SectionName</key>
|
206
|
+
|
207
|
+
<string>Section2</string>
|
208
|
+
|
209
|
+
<key>Items</key>
|
210
|
+
|
211
|
+
<array>
|
212
|
+
|
213
|
+
<dict>
|
214
|
+
|
215
|
+
<key>Image</key>
|
216
|
+
|
217
|
+
<string>1.MinnieIcon</string>
|
218
|
+
|
219
|
+
<key>Name</key>
|
220
|
+
|
221
|
+
<string>Minnie</string>
|
222
|
+
|
223
|
+
</dict>
|
224
|
+
|
225
|
+
<dict>
|
226
|
+
|
227
|
+
<key>Image</key>
|
228
|
+
|
229
|
+
<string>2.DonaldIcon</string>
|
230
|
+
|
231
|
+
<key>Name</key>
|
232
|
+
|
233
|
+
<string>Donald</string>
|
234
|
+
|
235
|
+
</dict>
|
236
|
+
|
237
|
+
<dict>
|
238
|
+
|
239
|
+
<key>Image</key>
|
240
|
+
|
241
|
+
<string>3.GoofyIcon</string>
|
242
|
+
|
243
|
+
<key>Name</key>
|
244
|
+
|
245
|
+
<string>Goofy</string>
|
246
|
+
|
247
|
+
</dict>
|
248
|
+
|
249
|
+
<dict>
|
250
|
+
|
251
|
+
<key>Image</key>
|
252
|
+
|
253
|
+
<string>4.PlutoIcon</string>
|
254
|
+
|
255
|
+
<key>Name</key>
|
256
|
+
|
257
|
+
<string>Pluto</string>
|
258
|
+
|
259
|
+
</dict>
|
260
|
+
|
261
|
+
</array>
|
262
|
+
|
263
|
+
</dict>
|
264
|
+
|
265
|
+
</array>
|
266
|
+
|
267
|
+
</plist>
|
268
|
+
|
269
|
+
|
16
270
|
|
17
271
|
```
|
18
272
|
|
19
273
|
|
20
274
|
|
21
|
-
###
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
// Plistファイルパス
|
44
|
-
|
45
|
-
let path = Bundle.main.path(forResource: "DisneyCharacterList", ofType:"plist")
|
46
|
-
|
47
|
-
_items = NSArray(contentsOfFile:path!)!
|
48
|
-
|
49
|
-
//println(_items);
|
50
|
-
|
51
|
-
}
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
// 設定
|
56
|
-
|
57
|
-
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
|
58
|
-
|
59
|
-
return 1
|
60
|
-
|
61
|
-
}
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
// 設定(行数)
|
66
|
-
|
67
|
-
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int)
|
68
|
-
|
69
|
-
-> Int {
|
70
|
-
|
71
|
-
return _items.count
|
72
|
-
|
73
|
-
}
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
// 設定(セル)
|
78
|
-
|
79
|
-
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
let cell = tableView.dequeueReusableCell(withIdentifier: "DisneyCell", for: indexPath as IndexPath)
|
84
|
-
|
85
|
-
let dic = _items.object(at: indexPath.row) as! NSDictionary
|
86
|
-
|
87
|
-
cell.textLabel!.text = dic.value(forKey: "Name") as? String
|
88
|
-
|
89
|
-
let imageName = dic.value(forKey: "Image") as! String
|
90
|
-
|
91
|
-
cell.imageView?.image = UIImage(named: imageName)
|
92
|
-
|
93
|
-
return cell
|
94
|
-
|
95
|
-
}
|
96
|
-
|
97
|
-
//セルタップ時にセルの内容を取得する
|
98
|
-
|
99
|
-
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
100
|
-
|
101
|
-
let cell = tableView.dequeueReusableCell(withIdentifier: "DisneyCell", for: indexPath as IndexPath)
|
102
|
-
|
103
|
-
let dic = _items.object(at: indexPath.row) as! NSDictionary
|
104
|
-
|
105
|
-
cell.textLabel!.text = dic.value(forKey: "Name") as? String
|
106
|
-
|
107
|
-
print(cell.textLabel!.text as Any)
|
108
|
-
|
109
|
-
}
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
//セクション数の設定
|
114
|
-
|
115
|
-
func numberOfSections(in tableView: UITableView) -> Int {
|
116
|
-
|
117
|
-
return 2
|
118
|
-
|
119
|
-
}
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
//セクション名設定
|
124
|
-
|
125
|
-
let sectionTitles = ["セクション1","セクション2"]
|
126
|
-
|
127
|
-
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
|
128
|
-
|
129
|
-
{
|
130
|
-
|
131
|
-
return sectionTitles[section]
|
132
|
-
|
133
|
-
}
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
}
|
138
|
-
|
139
|
-
```
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
AssetCatalog
|
144
|
-
|
145
|
-
![イメージ説明](0206d53867295e0fe18902dacf064ec0.png)
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
info.Plist
|
150
|
-
|
151
|
-
![イメージ説明](7746cbca09a76c1ff8f7c374534a98e9.png)
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
```DisneyCharacterListPlist
|
156
|
-
|
157
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
158
|
-
|
159
|
-
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
160
|
-
|
161
|
-
<plist version="1.0">
|
162
|
-
|
163
|
-
<array>
|
164
|
-
|
165
|
-
<dict>
|
166
|
-
|
167
|
-
<key>SectionName</key>
|
168
|
-
|
169
|
-
<string>Section1</string>
|
170
|
-
|
171
|
-
<key>Items</key>
|
172
|
-
|
173
|
-
<array>
|
174
|
-
|
175
|
-
<dict>
|
176
|
-
|
177
|
-
<key>Image</key>
|
178
|
-
|
179
|
-
<string>0.MickeyIcon</string>
|
180
|
-
|
181
|
-
<key>Name</key>
|
182
|
-
|
183
|
-
<string>Mickey</string>
|
184
|
-
|
185
|
-
</dict>
|
186
|
-
|
187
|
-
</array>
|
188
|
-
|
189
|
-
</dict>
|
190
|
-
|
191
|
-
<dict>
|
192
|
-
|
193
|
-
<key>SectionName</key>
|
194
|
-
|
195
|
-
<string>Section2</string>
|
196
|
-
|
197
|
-
<key>Items</key>
|
198
|
-
|
199
|
-
<array>
|
200
|
-
|
201
|
-
<dict>
|
202
|
-
|
203
|
-
<key>Image</key>
|
204
|
-
|
205
|
-
<string>1.MinnieIcon</string>
|
206
|
-
|
207
|
-
<key>Name</key>
|
208
|
-
|
209
|
-
<string>Minnie</string>
|
210
|
-
|
211
|
-
</dict>
|
212
|
-
|
213
|
-
<dict>
|
214
|
-
|
215
|
-
<key>Image</key>
|
216
|
-
|
217
|
-
<string>2.DonaldIcon</string>
|
218
|
-
|
219
|
-
<key>Name</key>
|
220
|
-
|
221
|
-
<string>Donald</string>
|
222
|
-
|
223
|
-
</dict>
|
224
|
-
|
225
|
-
<dict>
|
226
|
-
|
227
|
-
<key>Image</key>
|
228
|
-
|
229
|
-
<string>3.GoofyIcon</string>
|
230
|
-
|
231
|
-
<key>Name</key>
|
232
|
-
|
233
|
-
<string>Goofy</string>
|
234
|
-
|
235
|
-
</dict>
|
236
|
-
|
237
|
-
<dict>
|
238
|
-
|
239
|
-
<key>Image</key>
|
240
|
-
|
241
|
-
<string>4.PlutoIcon</string>
|
242
|
-
|
243
|
-
<key>Name</key>
|
244
|
-
|
245
|
-
<string>Pluto</string>
|
246
|
-
|
247
|
-
</dict>
|
248
|
-
|
249
|
-
</array>
|
250
|
-
|
251
|
-
</dict>
|
252
|
-
|
253
|
-
</array>
|
254
|
-
|
255
|
-
</plist>
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
```
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
### 試したこと
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
ここに問題に対して試したことを記載してください。
|
275
|
+
### 参考サイト
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
セクションとセルをViewDidload内で関数で指定する方法は検索するとよく出てくるのですが、
|
280
|
+
|
281
|
+
ネストした構造のplistから取得して表示する方法がみつからなかったです…
|
282
|
+
|
283
|
+
[[iPhone] UITableView Section の設定](https://i-app-tec.com/ios/tableview-section.html)
|
284
|
+
|
285
|
+
[【Swift】複数の section がある tableView で二次元配列を使う。](https://qiita.com/BMJr/items/ca7bcf76d36acbdef75e)
|
286
|
+
|
287
|
+
[【初心者向け】UITableViewのSection(セクション)の使い方](https://blog.mothule.com/ios/uitableview/ios-uitableview-section-basic)
|
288
|
+
|
289
|
+
[Swift4 でTableViewを使ってみる2 「セクション機能を使いこなそう」](https://note.com/mega_gorilla/n/nc2ca88f88d2e)
|
290
|
+
|
291
|
+
[【Objective-C】【Swift】TableViewの基本設定について](https://qiita.com/eito_2/items/374c440ae8b3e3c93178)
|
292
|
+
|
293
|
+
[[swift]ネストしたDictionaryの取得](https://qiita.com/kohei1218/items/a68deeff3add4b2a2d6e)
|