UICollectionView 未显示,未调用 cellForItemAt 和 numberOfItemsInSection

UICollectionView not displaying, cellForItemAt and numberOfItemsInSection not called

我正在尝试制作一个应用程序,该应用程序将显示带有“右”或“左”名称的设备的弹出窗口,具体取决于按下的是左按钮还是右按钮。但是,在我按下向左或向右按​​钮后,弹出窗口出现空的 CollectionView。我将其涂成绿色以确保它的大小不是 {0, 0} 而事实并非如此。

Popup screen

此外,我试图确保单元格不大于 CollectionView,而且看起来它们不是。不过,我不能对它们施加尺寸限制,所以它们可能会调整尺寸或其他东西。

Storyboard

我尝试理解并使用 的解决方案,但我的情况似乎有些不同。在这个问题中调用了 cellForItemMethod,但在我的例子中它没有,而且我没有 UICollectionView 的子类,我想这就是为什么我不能覆盖 intrinsicContentSize 的原因。

这是 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
        cell.setSelected()
        lastSelectedCell?.setUnselected()
        lastSelectedCell = cell
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        print("parent has ", parentVC?.rightDevices.count, " items")
        collectionView.reloadData()
    }

    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)
    }
    
}

再次检查 leftDevices 和 rightDevices 数据在 ViewController 之间传递到 PopUpController 屏幕并符合委托和数据源。

collectionView.delegate = self
collectionView.dataSource = self