UICollectionView not displaying, cellForItemAt and numberOfItemsInSection not called

I’m trying to make an app which will display a popup with of devices with "right" or "left" names depending on whether left or right button was pressed. However, after I press left or right button, the popup appears with empty CollectionView. I colored it green to make sure that it’s size is not {0, 0} and it isn’t.

Popup screen

Also, I tried to make sure that cells are not bigger than the CollectionView, and it seems, that they are not. I can’t put size restraints on them, though, so maybe they get resized or something.


I tried understanding and using solution for that question, but it seems that my situation is somewhat different. In this question cellForItemMethod was called, but in my case it wasn’t and I’ve got no subclass of UICollectionView, which is, I guess, why I cant override intrinsicContentSize.

Here is the code for PopUpViewController:

import UIKit

protocol PopUpDelegate {
    func connect(device: CollectionCell)

class PopUpController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    static let identifier: String = "PopUpController"
    var parentVC: ViewController?
    var left: Bool?
    let reuseIdentifier = "cell"
    var delegate: PopUpDelegate?
    var lastSelectedCell: CollectionCell? = nil
    @IBOutlet weak var collectionView: UICollectionView!
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print("number of items in section called")
        if (left!) {
            return (parentVC!.leftDevices.count)
        } else {
            return (parentVC!.rightDevices.count)
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        print("cell for item called")
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! CollectionCell
        print("left: ",  left!)
        if (left!) {
            cell.displayDevice(device: parentVC!.leftDevices[indexPath.row])
        } else {
            cell.displayDevice(device: parentVC!.rightDevices[indexPath.row])
        cell.backgroundColor = UIColor.white
        cell.selectButton.setTitleColor(UIColor.black, for: UIControl.State.normal)
        cell.selectButton.setImage(UIImage(named: "unselectedButton"), for: UIControl.State.normal)
        return cell
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("You selected cell #\(indexPath.item)!")
        let cell = collectionView.cellForItem(at: indexPath) as! CollectionCell
        lastSelectedCell = cell
    override func viewDidLoad() {
        // Do any additional setup after loading the view.
        print("parent has ", parentVC?.rightDevices.count, " items")

    static func showPopup(parentVC: UIViewController, left: Bool){
        //creating a reference for the dialogView controller
        if let popupViewController = UIStoryboard(name: "PopUp", bundle: nil).instantiateViewController(withIdentifier: "PopUpController") as? PopUpController {
        popupViewController.modalPresentationStyle = .custom
        popupViewController.modalTransitionStyle = .crossDissolve
        //setting the delegate of the dialog box to the parent viewController
        popupViewController.delegate = parentVC as? PopUpDelegate
        popupViewController.left = left
            parentVC = parentVC as? ViewController
            print("showing left popup: ", left)
        //presenting the pop up viewController from the parent viewController
        parentVC.present(popupViewController, animated: true)
    @IBAction func onConnectPressed(_ sender: Any) {
        self.delegate?.connect(device: lastSelectedCell!)
        self.dismiss(animated: true, completion: nil)
    @IBAction func onDismissPressed(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)

>Solution :

Check again leftDevices and rightDevices data passing between ViewController to PopUpController screen and conform delegate and datasource methods.

collectionView.delegate = self
collectionView.dataSource = self

Leave a Reply