【问题标题】:How to create Tableview hierarchy from JSON?如何从 JSON 创建 Tableview 层次结构?
【发布时间】:2019-07-03 07:30:19
【问题描述】:

我正在尝试构建一个简单的应用程序,但令人惊讶的是我找不到任何可以帮助我的答案。

根视图将是一个 UINavigationController,我想在其中放置一个 UITableViewController,我将从字典中的数据中填充 UITableViewCells。当我单击其中一个 UITableViewCell 时,它也会将我重定向到另一个从我的字典中填充的 UITableViewController。

[
        "name": "Functions",
        "icon": "calculator",
        "subcategories": [
            [
                "name": "Plan Leg",
                "icon": "globe",
                "subcategories": [
                    [
                        "name": "Heading & Groundspeed",
                        "destination": ""
                    ],
                    [
                        "name": "Time & Distance",
                        "destination": ""
                    ],
                    [
                        "name": "Fuel & Distance",
                        "destination": ""
                    ],
                ]
            ]
       ]
]

例如,我想创建一个名为“Functions”的 TableViewController,其中一个 TableViewCell“Plan Leg”重定向到另一个 TableViewController,其中包含 3 个单元格“Heading & Groundspeed”、“Time & Distance”和“Fuel &距离”

【问题讨论】:

    标签: ios json swift uitableview uinavigationcontroller


    【解决方案1】:
    class CatagoriesArray : Decodable {
        var catagories : [Catagories]?
    }
    class Catagories : Decodable {
        var name : String?
        var icon : String? 
        var subcategories : [Subcategories]?
    }
    
    class Subcategories : Decodable {
        var name : String? 
        var icon : String? 
        var subcategories : [SubcategoriesType]?
     }
    
     class SubcategoriesType : Decodable {
        var name : String? 
        var destination : String?
     }
    

    添加这一行进行解码

     let model = try? JSONDecoder().decode(CatagoriesArray.self, from: data) // data that will come from API response 
    

    在“函数”tableViewController中

    override func viewDidLoad() {
        self.title = model[0].name // prints "Functions"
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return model[0].subcategories.count 
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath) 
        cell.nameLabel.text = model[0].subcategories.name
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = self.storyBoard.instantiateViewController(withIdentifier : "identifier") as! FuncCatagory
        vc.subcategories = model[0].subcategories[indexPath.row]
        self.navigationController.pushViewController(vc, animated : true)
    }
    

    FuncCatagory 中填充tableview 就像相同的代码

    【讨论】:

      猜你喜欢
      • 2011-11-16
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2013-06-07
      • 1970-01-01
      • 2018-11-19
      • 2017-04-18
      相关资源
      最近更新 更多