Home > OS >  How to show array of array json in tableview using Swift?
How to show array of array json in tableview using Swift?

Time:09-17

Now my ViewController code is here -

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 only the "name" is being displayed in the tableview. But the "value" is not coming. The output I'm getting like this. Please guide me. Thanks in advance.

enter image description here

CodePudding user response:

Replace

        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 contains top category name

            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
         

You don't have to maintain 2 arrays it's only one

CodePudding user response:

Replace

    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
        }
    }

With

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
    }
  • Related