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.
Pass indexPath value in Cell class return back indexPath on Button Click function
protocol:-
cellForItemAt:
cell:
get cell using :-
The best way to do is in protocol method send back the cell.. for example
}
and on button click
and on you viewcontroller when delegate method implemented
You can check with button tag in
cellForItemAt:
Put a variable in your cell and save your session data model instance in
cellForRowAt
. Then update your delegate methods and add for each method a session parameter.So you have the instance of your data model for each button press
You can achieve it by giving tags to all button in cellForItemAt: as bellow
And get index back by :