Search code examples
swiftuinavigationcontrollerstoryboard

Navigate one collection view controller to another collection view controller?


I am trying to navigate one collection view controller to another collection view controller,Because I want to show my images one collection view to another collection view by using navigation. Is there any possibility to do that? Please help me


Solution

  • I did by my own... here I am attaching my code 1st I created struct

    import Foundation
    struct City {
        var image:String = ""
        var name:String = ""
        
        init(image: String, name: String){
            self.image = image
            self.name = name
        }
    }
    

    after that I enter code for my 1st cell view controller

    import UIKit
    
        private let reuseIdentifier = "Cell"
        
        class cityCollectionViewController: UICollectionViewController {
            var cities : [City] = [ City(image: "Ankara", name: "Ankara"),
                                                City(image: "Antalya", name: "Antalya"),
                                                City(image: "Aydin", name: "Aydin"),
                                                City(image: "Bodrum", name: "Bodrum"),
                                                City(image: "Canakkale", name: "Canakkale"),
                                                City(image: "Cappadocia", name: "Cappadocia"),
                                                City(image: "Efes", name: "Efes"),
                                                City(image: "Eskisehir", name: "Eskisehir"),
                                                City(image: "Fethiye", name: "Fethiye"),
                                                City(image: "Istanbul", name: "Istanbul"),
                                                City(image: "Izmir", name: "Izmir"),
                                                City(image: "Mardin", name: "Mardin"),
                                                City(image: "Nemrut", name: "Nemrut"),
                                                ]
        
            override func viewDidLoad() {
                super.viewDidLoad()
        
            }
        
            
        
            // MARK: UICollectionViewDataSource
        
            override func numberOfSections(in collectionView: UICollectionView) -> Int {
                
                return 1
            }
        
        
            override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
                
                return cities.count
            }
        
            override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                var cell = collectionView.dequeueReusableCell(withReuseIdentifier: "dataCell", for: indexPath) as! cityCollectionViewCell
                    
                        
                cell.cityImageView.image = UIImage(named: cities[indexPath.row].image )
                cell.cityNameLabel.text = cities[indexPath.row].name
                    
                        return cell
            }
            override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
                    let imageDetailVC = self.storyboard?.instantiateViewController(withIdentifier: "collectCollectionViewController") as! collectCollectionViewController
        
        /
                    self.navigationController?.pushViewController(imageDetailVC, animated: true)
               
                }
    
           
            
        
        
        
        }
    

    created a data cell and gave the outlets

    import UIKit
    
    class cityCollectionViewCell: UICollectionViewCell {
        @IBOutlet weak var cityImageView: UIImageView!
           @IBOutlet weak var cityNameLabel: UILabel!
        
    }
    

    This is for my second cell view controller

    import UIKit
    
    private let reuseIdentifier = "Cell"
    
    class collectCollectionViewController: UICollectionViewController {
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
            
            
        }
    
       var obj = cityCollectionViewController()
        var app1 = secondCollectionViewCell()
        override func numberOfSections(in collectionView: UICollectionView) -> Int {
            
            return 1
        }
    
    
        override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            
    
            return obj.cities.count
        }
    
        override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Datacell1", for: indexPath) as! secondCollectionViewCell
            let city1 = obj.cities[indexPath.row]
            cell.image11.image = UIImage(named: obj.cities[indexPath.row].image)
            cell.label11.text = obj.cities[indexPath.row].name
            
    
            return cell
        }
    
    
        
    
    }
    

    This code for my second data view cell

    import UIKit
    
    class secondCollectionViewCell: UICollectionViewCell {
        @IBOutlet weak var image11: UIImageView!
        @IBOutlet weak var label11: UILabel!
    }