Digs
Digs

Reputation: 193

How to parse Json file in swift for 2 collection Views?

I have JSON file like this

{
  "dicts": [
    {
      "main": "",
      "note1": "",
      "note2": "",
      "note3": "",
      "note4": ""
    },
    {
      "main": "",
      "note1": "",
      "note2": "",
      "note3": "",
      "note4": "",
      "note5": ""
    },
    {
      "main": "",
      "note1": "",
      "note2": ""
    }
  ]
}

My App has 2 view controllers each having collection view. In first collection view I will display JSON data main for each cell and when I click main I have trouble setting note data for that respective main content in each cell. All I came able to call any one note value.

Model:

struct SecPage {
    var note1:String?
    var note1:String?
    var note2:String?
    var note3:String?
    var note4:String?
    var note5:String?
}

static func downSec () -> [SecPage] {
    let jsonFile = Bundle.main.url(forResource: "info", withExtension: "json")
    let jsonData = try? Data(contentsOf: jsonFile!)
    var noteArray = [SecPage]()

    do{
        if let jsonResult = try JSONSerialization.jsonObject(with: jsonData!, options: .mutableContainers) as? Dictionary<String,AnyObject>{
            let name = jsonResult["dicts"] as? [Dictionary<String,AnyObject>]
            for note in name!{
                let note1 = note["note1"] as? String
                let note2 = note["note2"] as? String
                let note3 = note["note3"] as? String
                let note4 = note["note4"] as? String
                let note5 = note["note5"] as? String
                let noteinfo = SecPage(note1: note1 ,note2:    note2,note3: note3, note4: note4, note5: note5, note6: note6)
                noteArray.append(noteinfo)    }}  } catch{print("note not found")}
    return noteArray
}

View

class CollectionViewCell2: UICollectionViewCell {
    @IBOutlet weak var img: UIImageView!
    @IBOutlet weak var names: UILabel!

    func updateUIsec(data:dataObj)  {
        names.text = data.note1
    }
}

How to update in cell when I can able to enter only one data in name.text and how to efficiently use Model class to retrieve json file and send to view.

Upvotes: 1

Views: 631

Answers (1)

nathan
nathan

Reputation: 9395

I'm still unsure if this is what you are looking for, your question is too broad and hard to understand. Comment the answer and I'll update it as needed ;)

VC1:

class VC1: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    @IBOutlet weak var collectionView: UICollectionView!
    var modelData = [SecPage]()
    var selectedItem: SecPage?

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.delegate   = self
        collectionView.dataSource = self

        //collectionView.register... // Register UICollectionView cell subclass
        modelData = SecPage.downSec()
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let secPage = modelData[indexPath.row]
        let cell = collectionView.dequeue...
        cell.textLabel.text = secPage.main

        return cell
    }

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

        if let destination = segue.destination as? VC2{
            destination.selectedItem = selectedItem
        }
    }

    override func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {

        selectedItem = modelData[indexPath.row]
        performSegue...
    }
}

VC2:

class VC2: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    @IBOutlet weak var collectionView: UICollectionView!
    var selectedItem: SecPage? = nil

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.delegate   = self
        collectionView.dataSource = self

        //collectionView.register... // Register UICollectionView cell subclass
        modelData = SecPage.downSec()
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeue...
        cell.updateUIsec(data: selectedItem!)

        return cell
    }
}

Cell:

class CollectionViewCell2: UICollectionViewCell {
    @IBOutlet weak var img: UIImageView!
    @IBOutlet weak var names: UILabel!

    func updateUIsec(data: SecPage)  {
        names.text = data.note1
    }
}

Upvotes: 1

Related Questions