App crashing while using collection view

I was trying to have a secondary collection view with circle images above (y axis) the functioning collection view but its crashing and throwing this error

Error code: could not dequeue a view of kind: UICollectionElementKindCell with identifier CircleCollectionViewCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard

ViewController code: //

//  ViewController.swift

//  Main_app

//

//  Created by Kushagr Agarwal on 23/08/21.

//

import UIKit

class ViewController: UIViewController {

    @IBOutlet var collectionView: UICollectionView!

    

    @IBOutlet var topCollectionView: UICollectionView!

    

    @IBOutlet weak var Leading: NSLayoutConstraint!

    

    @IBOutlet weak var Trailing: NSLayoutConstraint!

    

    @IBOutlet private weak var Menubutton: UIButton!

    var menuOut = false

    

    

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.

        //self.navigationItem.titleView = UIImageView(image: UIImage(named: "Logo"))

        

        collectionView.register(MiddleCollectionViewCell.nib(), forCellWithReuseIdentifier: MiddleCollectionViewCell.identifier)

        

        collectionView.delegate=self

        collectionView.dataSource=self

        

        topCollectionView.register(CircleCollectionViewCell.self, forCellWithReuseIdentifier: CircleCollectionViewCell.identifier)

        topCollectionView.delegate=self

        topCollectionView.dataSource=self

//       Menu

//       start

        

        let destructiveAction = UIAction(title: "Delete",image: UIImage(systemName: "nosign") , attributes: .destructive) { (_) in

            print("Delete")

        }

        

        let menu = UIMenu(title: "", children: [

            UIAction (title: "Add New", image: UIImage(systemName: "plus.circle"), handler: { _ in  }),

            UIAction (title: "Manage", image: UIImage(systemName: "hammer"), handler: { _ in  }),

            destructiveAction

        ])

        self.Menubutton.menu = menu

//    Menu end

    }

// Menu animation

    @IBAction func MenuTap(_ sender: Any) {

        if menuOut == false {

            Leading.constant = -150

            Trailing.constant = 150

            menuOut = true

        }

        else{

            Leading.constant = 0

            Trailing.constant = 0

            menuOut = false

        }

        UIView.animate(withDuration: 0.2, delay: 0.0 , options: .curveEaseIn,animations: {

            self.view.layoutIfNeeded()

        }){(animationComplete) in

            print("Animation Completed")

        }

    }

// Menu animation ends

}

// used for upper collection view

extension ViewController: UICollectionViewDelegate {

    

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        collectionView.deselectItem(at: indexPath, animated: true )

        print("btn tapped")

        

    }

    

    

}

extension ViewController: UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return 12

    }

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

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MiddleCollectionViewCell.identifier, for: indexPath) as! MiddleCollectionViewCell

        let topcell = collectionView.dequeueReusableCell(withReuseIdentifier: CircleCollectionViewCell.identifier, for: indexPath) as! CircleCollectionViewCell

        topcell.configure(with: "")

        

        cell.configure(with: UIImage(named: "four")!)

        return cell; topcell

    }

}

//extension ViewController: UICollectionViewDelegateFlowLayout {

//

//}

Answered by OOPer in 686747022

Please use Code Block when you show some code:

import UIKit
class ViewController: UIViewController {
    @IBOutlet var collectionView: UICollectionView!
    
    @IBOutlet var topCollectionView: UICollectionView!
    
    @IBOutlet weak var Leading: NSLayoutConstraint!
    
    @IBOutlet weak var Trailing: NSLayoutConstraint!
    
    @IBOutlet private weak var Menubutton: UIButton!
    var menuOut = false
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        //self.navigationItem.titleView = UIImageView(image: UIImage(named: "Logo"))
        
        collectionView.register(MiddleCollectionViewCell.nib(), forCellWithReuseIdentifier: MiddleCollectionViewCell.identifier)
        
        collectionView.delegate=self
        collectionView.dataSource=self
        
        topCollectionView.register(CircleCollectionViewCell.self, forCellWithReuseIdentifier: CircleCollectionViewCell.identifier)
        topCollectionView.delegate=self
        topCollectionView.dataSource=self
//       Menu
//       start
        
        let destructiveAction = UIAction(title: "Delete",image: UIImage(systemName: "nosign") , attributes: .destructive) { (_) in
            print("Delete")
        }
        
        let menu = UIMenu(title: "", children: [
            UIAction (title: "Add New", image: UIImage(systemName: "plus.circle"), handler: { _ in  }),
            UIAction (title: "Manage", image: UIImage(systemName: "hammer"), handler: { _ in  }),
            destructiveAction
        ])
        self.Menubutton.menu = menu
//    Menu end
    }
// Menu animation
    @IBAction func MenuTap(_ sender: Any) {
        if menuOut == false {
            Leading.constant = -150
            Trailing.constant = 150
            menuOut = true
        }
        else{
            Leading.constant = 0
            Trailing.constant = 0
            menuOut = false
        }
        UIView.animate(withDuration: 0.2, delay: 0.0 , options: .curveEaseIn,animations: {
            self.view.layoutIfNeeded()
        }){(animationComplete) in
            print("Animation Completed")
        }
    }
// Menu animation ends
}
// used for upper collection view
extension ViewController: UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true )
        print("btn tapped")
        
    }
    
    
}
extension ViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 12
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MiddleCollectionViewCell.identifier, for: indexPath) as! MiddleCollectionViewCell
        let topcell = collectionView.dequeueReusableCell(withReuseIdentifier: CircleCollectionViewCell.identifier, for: indexPath) as! CircleCollectionViewCell
        topcell.configure(with: "")
        
        cell.configure(with: UIImage(named: "four")!)
        return cell; topcell
    }
}
//extension ViewController: UICollectionViewDelegateFlowLayout {
//
//}

You register CircleCollectionViewCell.self only for topCollectionView. So, calling collectionView.dequeueReusableCell(withReuseIdentifier: CircleCollectionViewCell.identifier, for: indexPath) causes the error when called where collectionView is not topCollectionView.

You cannot return two things from a single return statement, return cell; topcell does not make sense and ; topcell is ignored.

(You should better not ignore the warnings shown by Xcode.)

You may need to check if collectionView is topCollectionView or not inside delegate methods:

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if collectionView === self.topCollectionView {
            let topcell = collectionView.dequeueReusableCell(withReuseIdentifier: CircleCollectionViewCell.identifier, for: indexPath) as! CircleCollectionViewCell
            topcell.configure(with: "")
            return topcell
        } else {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MiddleCollectionViewCell.identifier, for: indexPath) as! MiddleCollectionViewCell
            
            cell.configure(with: UIImage(named: "four")!)
            return cell
        }
    }
Accepted Answer

Please use Code Block when you show some code:

import UIKit
class ViewController: UIViewController {
    @IBOutlet var collectionView: UICollectionView!
    
    @IBOutlet var topCollectionView: UICollectionView!
    
    @IBOutlet weak var Leading: NSLayoutConstraint!
    
    @IBOutlet weak var Trailing: NSLayoutConstraint!
    
    @IBOutlet private weak var Menubutton: UIButton!
    var menuOut = false
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        //self.navigationItem.titleView = UIImageView(image: UIImage(named: "Logo"))
        
        collectionView.register(MiddleCollectionViewCell.nib(), forCellWithReuseIdentifier: MiddleCollectionViewCell.identifier)
        
        collectionView.delegate=self
        collectionView.dataSource=self
        
        topCollectionView.register(CircleCollectionViewCell.self, forCellWithReuseIdentifier: CircleCollectionViewCell.identifier)
        topCollectionView.delegate=self
        topCollectionView.dataSource=self
//       Menu
//       start
        
        let destructiveAction = UIAction(title: "Delete",image: UIImage(systemName: "nosign") , attributes: .destructive) { (_) in
            print("Delete")
        }
        
        let menu = UIMenu(title: "", children: [
            UIAction (title: "Add New", image: UIImage(systemName: "plus.circle"), handler: { _ in  }),
            UIAction (title: "Manage", image: UIImage(systemName: "hammer"), handler: { _ in  }),
            destructiveAction
        ])
        self.Menubutton.menu = menu
//    Menu end
    }
// Menu animation
    @IBAction func MenuTap(_ sender: Any) {
        if menuOut == false {
            Leading.constant = -150
            Trailing.constant = 150
            menuOut = true
        }
        else{
            Leading.constant = 0
            Trailing.constant = 0
            menuOut = false
        }
        UIView.animate(withDuration: 0.2, delay: 0.0 , options: .curveEaseIn,animations: {
            self.view.layoutIfNeeded()
        }){(animationComplete) in
            print("Animation Completed")
        }
    }
// Menu animation ends
}
// used for upper collection view
extension ViewController: UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true )
        print("btn tapped")
        
    }
    
    
}
extension ViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 12
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MiddleCollectionViewCell.identifier, for: indexPath) as! MiddleCollectionViewCell
        let topcell = collectionView.dequeueReusableCell(withReuseIdentifier: CircleCollectionViewCell.identifier, for: indexPath) as! CircleCollectionViewCell
        topcell.configure(with: "")
        
        cell.configure(with: UIImage(named: "four")!)
        return cell; topcell
    }
}
//extension ViewController: UICollectionViewDelegateFlowLayout {
//
//}

You register CircleCollectionViewCell.self only for topCollectionView. So, calling collectionView.dequeueReusableCell(withReuseIdentifier: CircleCollectionViewCell.identifier, for: indexPath) causes the error when called where collectionView is not topCollectionView.

You cannot return two things from a single return statement, return cell; topcell does not make sense and ; topcell is ignored.

(You should better not ignore the warnings shown by Xcode.)

You may need to check if collectionView is topCollectionView or not inside delegate methods:

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if collectionView === self.topCollectionView {
            let topcell = collectionView.dequeueReusableCell(withReuseIdentifier: CircleCollectionViewCell.identifier, for: indexPath) as! CircleCollectionViewCell
            topcell.configure(with: "")
            return topcell
        } else {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MiddleCollectionViewCell.identifier, for: indexPath) as! MiddleCollectionViewCell
            
            cell.configure(with: UIImage(named: "four")!)
            return cell
        }
    }
App crashing while using collection view
 
 
Q