【问题标题】:UICollectionView not displaying, cellForItemAt and numberOfItemsInSection not calledUICollectionView 未显示,未调用 cellForItemAt 和 numberOfItemsInSection
【发布时间】:2021-12-30 10:16:28
【问题描述】:

我正在尝试制作一个应用程序,该应用程序将显示一个弹出窗口,其中包含“右”或“左”名称的设备,具体取决于按下的是左键还是右键。但是,在我按下左键或右键后,弹出窗口显示为空的 CollectionView。我将它涂成绿色以确保它的大小不是 {0, 0} 并且不是。

Popup screen

另外,我试图确保单元格不大于 CollectionView,而且看起来它们不是。不过,我不能对它们设置大小限制,所以可能会调整它们的大小或其他什么。

Storyboard

我尝试理解和使用that question 的解决方案,但我的情况似乎有些不同。在这个问题中调用了 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)
    }
    
}

【问题讨论】:

  • 你是否在 storyboard 中为集合视图设置了委托和数据源?

标签: ios swift xcode uicollectionview


【解决方案1】:

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

collectionView.delegate = self
collectionView.dataSource = self

【讨论】:

    猜你喜欢
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    相关资源
    最近更新 更多