UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

parsing JSON

Forums > iOS

Hi all,

I'm having problems in parsing json data into a table view. At the moment I have a local json file and a couple of deecodable structs, I'm also using Pauls json extension. So the project has three screens, the first one being the category table view which is working fine. The second screen is another table view and this is where i get stuck. What I want to happen is when you tap the category cell, I want the table view to show the wineDetail information. I'm not to sure how i can call that in the table view. Many thanks in Thank you in advance

Iain

[
{
"categoryName": "White Wine",
"imageURL": "white",
"wineDetail" : [
    {
        "wineName": "Brancott Estate Sauvignon Blanc 75cl",
        "wineImg": "BrancottEstateSauvignonBlanc"
    }
],
},

{
"categoryName": "Red Wine",
"imageURL": "red",
"wineDetail" : [
    {
        "wineName": "Campo Viejo Rioja Tempranillo 75cl",
        "wineImg": "CampoVigo"
    }
],
},

{
"categoryName": "Rose Wine",
"imageURL": "rose",
"wineDetail" : [
    {
        "wineName": "Baron Gassier Provence St V Rose 75cl",
        "wineImg": "Baron"
    }
],
}
]

import Foundation

struct CategoryList: Decodable {

    let categoryName: String
    let imageURL: String
    let wineDetail : [WineDetail]

    static let allWineCategorys = Bundle.main.decode([CategoryList].self, from: "CategoryWine.json")
}

struct WineDetail: Decodable {
    let wineName: String
    let wineImg : String

}
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    var wines: [CategoryList] = []
    var selectedCategoryToPass: String!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        print("Iain **:\(CategoryList.allWineCategorys)")
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if let dv = segue.destination as? DetailWinesVC {
                dv.seletedCategoryToPass = selectedCategoryToPass
                dv.title = selectedCategoryToPass
            }
        }
        }

    extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return CategoryList.allWineCategorys.count

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CategoryCell
        let wine = CategoryList.allWineCategorys[indexPath.row]
        cell.configureCell(wineDetail: wine)

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        selectedCategoryToPass = CategoryList.allWineCategorys[indexPath.row].categoryName
        performSegue(withIdentifier: "ShowDetail", sender: self)
    }

}
import UIKit

class DetailWinesVC: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    var seletedCategoryToPass: String!

    var selectedDetail: WineDetail!

    var wineDetails: CategoryList!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let dv = segue.destination as? DetailVC {
            dv.selectedDetail = selectedDetail
        }
    }
}

extension DetailWinesVC: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return wineDetails.wineDetail.count

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! DetailWinesCell
        let wineDetail = wineDetails.wineDetail[indexPath.row]
        cell.configureCell(wineDetail: wineDetail)

        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedDetail = wineDetails.wineDetail[indexPath.row]
        performSegue(withIdentifier: "Detail", sender: self)
    }

}

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.