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.
In
cellForItemAt:
method you can specifytag
value and when a button is tapped you can checksender.tag
to check which button is tapped.