回答編集履歴
2
plistファイルのパースに PropertyListDecoder を使用するサンプルの追加
test
CHANGED
@@ -161,3 +161,149 @@
|
|
161
161
|
|
162
162
|
|
163
163
|
データの取り出し方が変わりますのでご注意ください。
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
---
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
追記2
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
> >セクションごとの名前とデータを持つことができます。
|
176
|
+
|
177
|
+
> >データの取り出し方が変わりますのでご注意ください。
|
178
|
+
|
179
|
+
> この場合、セルに表示する「Name」と「Image」を取得する際に、「セクション1」の中の「Name」と「Image」…と指定するということですかね。
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
それで良いと思います!
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
質問の内容からは大きく離れてしまいますが、もし私が実装するとすれば データの取り出し方には Decodable を使うかと思います。
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
```swift
|
192
|
+
|
193
|
+
struct HogeItem: Decodable {
|
194
|
+
|
195
|
+
let name: String
|
196
|
+
|
197
|
+
let imageName: String
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
struct HogeSection: Decodable {
|
204
|
+
|
205
|
+
let name: String
|
206
|
+
|
207
|
+
let items: [HogeItem]
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
var _items: [HogeSection] = []
|
214
|
+
|
215
|
+
```
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
データ表示は下記のように書くことができます。
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
```swift
|
224
|
+
|
225
|
+
override func viewDidLoad() {
|
226
|
+
|
227
|
+
super.viewDidLoad()
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
// Plistファイルパス
|
232
|
+
|
233
|
+
if let path = Bundle.main.path(forResource: "hoge", ofType:"plist" ) {
|
234
|
+
|
235
|
+
let data = try! Data(contentsOf: URL(fileURLWithPath: path))
|
236
|
+
|
237
|
+
_items = try! PropertyListDecoder().decode([HogeSection].self, from: data)
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
//...省略
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
// 設定
|
248
|
+
|
249
|
+
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
|
250
|
+
|
251
|
+
return _items.count
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
// 設定(行数)
|
258
|
+
|
259
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
260
|
+
|
261
|
+
return _items[section].items.count
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
// 設定(セクションタイトル)
|
268
|
+
|
269
|
+
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
270
|
+
|
271
|
+
return _items[section].name
|
272
|
+
|
273
|
+
}
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
// 設定(セル)
|
278
|
+
|
279
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
280
|
+
|
281
|
+
let item = _items[indexPath.section].items[indexPath.row]
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "DisneyCell", for: indexPath as IndexPath)
|
286
|
+
|
287
|
+
cell.textLabel?.text = item.name
|
288
|
+
|
289
|
+
cell.imageView?.image = UIImage(named: item.imageName)
|
290
|
+
|
291
|
+
return cell
|
292
|
+
|
293
|
+
}
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
//セルタップ時にセルの内容を取得する
|
298
|
+
|
299
|
+
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
300
|
+
|
301
|
+
let item = _items[indexPath.section].items[indexPath.row]
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
print(item.name)
|
306
|
+
|
307
|
+
}
|
308
|
+
|
309
|
+
```
|
1
セクション名もplistで定義する方法を追記しました
test
CHANGED
@@ -137,3 +137,27 @@
|
|
137
137
|
}
|
138
138
|
|
139
139
|
```
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
---
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
追記
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
> セクション名をplistで指定することは可能でしょうか?
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
可能です。下記のようにplistを定義すれば、セクションごとの名前とデータを持つことができます。
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
![イメージ説明](449ca4e022d036ebdae6334db3565b9b.png)
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
データの取り出し方が変わりますのでご注意ください。
|