iOS how know which button is pressed CollectionVie

2019-02-19 04:44发布

So I have a cells in a collectionview with 3 buttons in it. To trigger code with these buttons I have implemented a custom delegate. Now the code is being triggered, but I don't know from which cell the code triggered. How can I best implement this? Here is some of my code. Protocol:

protocol OverViewDelegate {
func registerButtonClicked()
func evaluateButtonClicked()
func overviewButtonClicked()
}

cellForItemAt:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "sessionCell", for: indexPath) as? SessionCollectionViewCell
    let session: SessionModel
    session = DebugData.shared.sessionArray[indexPath.row]

    cell?.sessionImage.image = #imageLiteral(resourceName: "carControl")
    cell?.sessionNameLabel.text = session.name
    cell?.sessionLocationLabel.text = session.location
    cell?.overViewDelegate = self

    return cell!
}

cell:

import UIKit

import IBAnimatable

@IBOutlet weak var sessionImage: UIImageView!
@IBOutlet weak var sessionNameLabel: UILabel!
@IBOutlet weak var sessionLocationLabel: UILabel!
@IBOutlet weak var sessionRegisterButton: AnimatableButton!
@IBOutlet weak var sessionOverviewButton: AnimatableButton!
@IBOutlet weak var sessionEvaluateButton: AnimatableButton!

var overViewDelegate: OverViewDelegate?

@IBAction func registerButtonClicked(_ sender: Any) {
    overViewDelegate?.registerButtonClicked()
}

@IBAction func overviewButtonClicked(_ sender: Any) {
    overViewDelegate?.overviewButtonClicked()
}

@IBAction func evaluateButtonClicked(_ sender: Any) {
    overViewDelegate?.evaluateButtonClicked()
}

Any help would be appriciated.

7条回答
女痞
2楼-- · 2019-02-19 05:14

In cellForItemAt: method you can specify tag value and when a button is tapped you can check sender.tag to check which button is tapped.

查看更多
登录 后发表回答