函数 collectionView 函数 didSelectItemAt 不起作用

Function collectionView function didSelectItemAt not work

我有这个故事板:

和此代码:

@IBOutlet weak var collectionView1: UICollectionView!
    @IBOutlet weak var collectionView2: UICollectionView!

    var selectedTipId: Int?
    var selectedleafletId: Int?

    @IBAction func TipDetailBtnPressed(_ sender: Any) {
        print("@@@@@@ \(selectedTipId) i \(selectedleafletId)")

        if selectedTipId == nil {
            print("Error message")
            return
        }

        showSubViewInContainerView(view: "TipDetailsView", parm: selectedTipId!)
    }

    @IBAction func TipDetailPDFBtnPressed(_ sender: Any) {
        // TODO: Dodać id wybranego slidera

        if selectedleafletId == nil {
            print("Error message")
            return
        }

        showSubViewInContainerView(view: "TipDetailsPDFView", parm: selectedleafletId!)
    }

    let tipObjectArray = [
        TipObject(id: 1, description: "Jakość frytek nas nie zadawala", image: UIImage(named: "a1.jpg")),
        TipObject(id: 2, description: "Kolor frytek jest niesatysfakcjonujący", image: UIImage(named: "a2.jpg")),
        TipObject(id: 3, description: "LOT i reklamacja", image: UIImage(named: "a3.jpg")),
        TipObject(id: 4, description: "Olej nie spełnia naszych oczekiwań", image: UIImage(named: "a4.jpg")),
        TipObject(id: 5, description: "jakiś fajny", image: UIImage(named: "a5.jpg"))
    ]

    let leafletsObjectArray = [
        LeafletsObject(id: 1, description: "AV-AddedValueFries-Ulotka", image: UIImage(named: "d1.jpg")),
        LeafletsObject(id: 2, description: "AV-AddedValueFries-Ulotka 23112", image: UIImage(named: "d2.jpg")),
        LeafletsObject(id: 3, description: "Ulotka", image: UIImage(named: "d3.jpg")),
        LeafletsObject(id: 4, description: "Fajna ulotka", image: UIImage(named: "d4.jpg")),
        ]


    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView1.dataSource = self
        collectionView1.delegate = self
        collectionView2.dataSource = self
        collectionView2.delegate = self
    }

    func showSubViewInContainerView(view: String, parm: Int){
        let viewController = self.parent as! MainViewControler
        viewController.showSubViewInContainerView(view: view, parms: parm)
    }
}

extension TipViewController: UICollectionViewDelegate, UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        if collectionView == collectionView1 {
            return tipObjectArray.count
        }
        else {
            return leafletsObjectArray.count
        }
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if collectionView == collectionView1 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell1", for: indexPath) as! TipCollectionViewCellTips
            cell.titleLabel.text = tipObjectArray[indexPath.item].description
            cell.imgView.image = tipObjectArray[indexPath.item].image
            return cell
        }
        else {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell2", for: indexPath) as! TipCollectionViewCellLeaflets
            cell.titleLabel2.text = leafletsObjectArray[indexPath.item].description
            cell.imgView2.image = leafletsObjectArray[indexPath.item].image
            return cell
        }
    }



      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("I am here")
            if collectionView == collectionView1 {
                selectedTipId = tipObjectArray[indexPath.item].id
                print(selectedTipId)
            }
            else {
            selectedleafletId = leafletsObjectArray[indexPath.item].id
            print(selectedleafletId)
        }
    }

collectionView1 - 左Collection视图 collectionView2 - 是正确的 Collection 视图

TipDetailBtnPressed 和 TipDetailPDFBtnPressed - 这是 uiimage 和文本标签下的按钮。

当我点击这个按钮时 - 我得到了结果: @@@@@@ 零我零 错误信息

函数 collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) - 从不显示:"I am here"

我有以下问题: 1. 为什么我没有消息"I am here"? 2. 然后我按下:TipDetailBtnPressed 和 TipDetailPDFBtnPressed - 为什么我不能在 selectedTipId 和 selectedleafletId 变量中获取值?我总是收到消息:@@@@@@ nil 和 nil 错误信息 ?

protocol YourVehicleViewCellDelegate: NSObjectProtocol {
func didPressEditButton()

}

 // Make delegate in your cell class

 weak var delegate:YourVehicleViewCellDelegate? = nil



 @IBAction func button1 Tapped(_ sender: Any) {
    print("write button pressed")

    delegate?.didPressEditButton()

}



// call that delegate in your controller class
   func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if collectionView == collectionView1 {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell1", for: indexPath) as! TipCollectionViewCellTips
      cell.delegate = self // here call your delegate
        cell.titleLabel.text = tipObjectArray[indexPath.item].description
        cell.imgView.image = tipObjectArray[indexPath.item].image
        return cell
    }