【问题标题】:How can I populate a table view based on a local json file?如何根据本地 json 文件填充表格视图?
【发布时间】: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


    【解决方案1】:

    很简单:

    在类之外创建两个结构

    struct Item: Decodable {
        let name: String
        let levels : [Level]
    }
    
    struct Level: Decodable {
        let start, end: String
    }
    

    和一个数据源数组

    var items = [Item]()
    

    viewDidLoad 中解码 JSON,假设包中的文件名为 items.json

    override func viewDidLoad() {
        super.viewDidLoad()
        do {
            let data = try Data(contentsOf: Bundle.main.url(forResource: "items", withExtension: "json")!)
            items = try JSONDecoder().decode([Item].self, from: data)
            tableView.reloadData()
        } catch { print(error) }
    }
    

    你可以删除numberOfSections,因为默认是1,其他方法是

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    
        cell.textLabel?.text = items[indexPath.row].name
        // do something with items[indexPath.row].levels
        return cell
    }
    

    【讨论】:

      【解决方案2】:

      你想用本地 json 文件填充 tableview,所以这里有一个简单易懂的答案,你只需要检查一下,

      http://vasundharavision.com/blog/ios/how-to-parse-json-from-File-and-url-in-swift-4-2

      我希望它对你有用。

      【讨论】:

      • 对不起,文章中的代码非常糟糕且过时。如果有原生的等价物,不要在 Swift 中使用 NS... 类。永远不要在 Swift 中使用 mutableContainers,这是没有意义的。不要使用value(forKey,除非你能解释为什么明确需要 KVC。根本不要使用NSURLConnection,它已被弃用多年。不要try!。添加适当的错误处理。
      猜你喜欢
      • 1970-01-01
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      • 2017-10-04
      相关资源
      最近更新 更多