【发布时间】:2019-01-31 08:36:37
【问题描述】:
我正在创建一个简单的游戏,并想创建一个关卡选择屏幕。作为保存所有级别的一种方式,我创建了一个 JSON 文件。我现在正在尝试使用 JSON 文件中的级别填充表格视图。
[
{
"name": "animals",
"levels": [
{"start": "cat", "end": "bat"},
{"start": "dog", "end": "pig"}
]
},
{
"name": "foods",
"levels": [
{"start": "grape", "end": "apple"}
]
}
]
我已经能够成功地基于数组填充表格,如下所示,但无法从 json 文件中弄清楚如何做到这一点。
import UIKit
var test = ["hello", "world"]
class PackTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return test.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = test[indexPath.row]
return cell
}
}
我想用这个 JSON 文件填充表格视图以实现显示名称的表格:
动物
食物
谢谢!
【问题讨论】:
标签: arrays json swift uitableview tableview