如何使用 Swift 在 tableview 中显示数组 json 的数组?

How to show array of array json in tableview using Swift?

现在我的 ViewController 代码在这里 -

import UIKit
struct jsonstruct: Codable {
    let name: String
    let meta_data: [Categories]
    
    enum CodingKeys: String, CodingKey {
        case name
        case meta_data
    }
}

struct Categories: Codable {
    let value: String
    
    enum CodingKeys: String, CodingKey {
        case value
    }
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet weak var tableView: UITableView!
    
    var arrdata : [jsonstruct] = [jsonstruct]()
    var categorydata : [Categories] = [Categories]()
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        getdata()
    }
    
    func getdata() {
        
        let url = URL(string: "https://mywebstaging.net/ab/garnier/wp-json/wc/v3/products?consumer_key=<key>&consumer_secret=<secret>")
        URLSession.shared.dataTask(with: url!) { (data, response, error) in
            do{if error == nil{
                self.arrdata = try JSONDecoder().decode([jsonstruct].self, from: data!)
                print(self.arrdata)
                DispatchQueue.main.async {
                     self.tableView.reloadData()
                }
            }catch{
                print("Error in get json data")
            }
            
        }.resume()
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
           if tableView == tableView {
            return arrdata.count
           }else{
            return categorydata.count
           }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            if tableView == tableView {
                let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellTableViewCell
                let getdata = arrdata[indexPath.row]
                cell.lblid.text = getdata.name
                return cell
            }
            else{
                let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellTableViewCell
                let getdatadesciption = self.categorydata[indexPath.row]
                cell.lblname.text = getdatadesciption.value
                return cell
            }
        }
}

Hare 只有“名称”显示在表视图中。但是“价值”并没有到来。我得到的输出是这样的。请指导我。提前致谢。

替换

        if tableView == tableView {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellTableViewCell
            let getdata = arrdata[indexPath.row]
            cell.lblid.text = getdata.name
            return cell
        }
        else{
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellTableViewCell
            let getdatadesciption = self.categorydata[indexPath.row]
            cell.lblname.text = getdatadesciption.value
            return cell
        }

with getdata.meta_data.first?.value 包含顶级类别名称

            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellTableViewCell
            let getdata = arrdata[indexPath.row]
            cell.lblid.text = getdata.name  
            cell.lblname.text = getdata.meta_data.first?.value
            return cell
         

你不必维护 2 个数组,它只是一个

替换

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            if tableView == tableView {
                return arrdata.count
            } else {
                return categorydata.count
            }
        }

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView == tableView {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellTableViewCell
            let getdata = arrdata[indexPath.row]
            cell.lblid.text = getdata.name
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellTableViewCell
            let getdatadesciption = self.categorydata[indexPath.row]
            cell.lblname.text = getdatadesciption.value
            return cell
        }
    }

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrdata.count
    }

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellTableViewCell
        let data = arrdata[indexPath.row]
        let category = data.meta_data.first
        cell.lblid.text = data.name
        cell.lblname.text = category.value
        return cell
    }