函数collectionView函数didSelectItem不起作用

时间:2018-06-05 08:00:25

标签: ios swift swift3 swift4

我有这个故事板: storyboard

和这段代码:

@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 View collectionView2 - 是正确的集合视图

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

当我点击此按钮时 - 我有结果: @@@@@@ nil我没有 错误消息

函数collectionView(_ collectionView:UICollectionView,didSelectItemAt indexPath:IndexPath) - 从不显示:“我在这里”

我有问题: 1.为什么我没有留言“我在这里”? 2.然后我按下:TipDetailBtnPressed和TipDetailPDFBtnPressed - 为什么我没有在selectedTipId和selectedleafletId变量中获取值?我总是收到消息:@@@@@@ nil和nil 错误信息 ?

1 个答案:

答案 0 :(得分:1)

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
    }
相关问题