iosswiftuicollectionviewpass-data

How can I parse data from a UIViewController having 2 CollectionViews with swift


How do I parse data from a UIViewController having 2 different UICollectionViews? I have 2 UICollectionViews in on UIViewController.

I have tired this using didSelectItemAt and performSegue but it can't parse the data to the other screen

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showLineup" {
        
            
        let gidilecekShowVC = segue.destination as! lineupViewerVC
        
        gidilecekShowVC.showLineup = selectLineup1
       
    } else if segue.identifier == "showLineup2" {
        
        let gidilecekShowVC = segue.destination as! lineupViewer2VC
        
        gidilecekShowVC.showLineup2 = selectLineup2
       
    }
}

I know didSelectItemAt is wrong, but I don't know what the correct one is.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
    selectLineup1 = ekranaYansitA1[indexPath.item]
    performSegue(withIdentifier: "showLineup", sender: nil)
    
    selectLineup2 = ekranaYansitA2[indexPath.item]
    performSegue(withIdentifier: "showLineup2", sender: nil)
  
}

I'm a beginner. Thank you in advance for your help


Solution

  • didSelectItemAt has a collectionView parameter which gives you the collection view instance which triggered the method.

    Assuming you have an IBOutlet named lineup1CollectionView, a reference to the first collection view you can perform a check

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if collectionView == lineup1CollectionView {
            selectLineup1 = ekranaYansitA1[indexPath.item]
            performSegue(withIdentifier: "showLineup", sender: nil)
        } else {
            selectLineup2 = ekranaYansitA2[indexPath.item]
            performSegue(withIdentifier: "showLineup2", sender: nil)
        }
    }