所以我在一个集合视图中有一个单元格,里面有 3 个按钮.为了使用这些按钮触发代码,我实现了一个自定义委托.现在代码正在被触发,但我不知道代码是从哪个单元格触发的.我怎样才能最好地实现这一点?这是我的一些代码.协议:
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:
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!
}
单元格:
import UIKit
导入 IBAnimatable
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.
在Cell类中传递indexPath值在Button Click函数中返回indexPath
Pass indexPath value in Cell class return back indexPath on Button Click function
协议:-
protocol OverViewDelegate {
func registerButtonClicked(_ indexPath : IndexPath)
func evaluateButtonClicked(_ indexPath : IndexPath)
func overviewButtonClicked(_ indexPath : IndexPath)
}
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
cell?.indexPath = indexPath
return cell!
}
单元格:
@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?
var indexPath : IndexPath?
@IBAction func registerButtonClicked(_ sender: Any) {
overViewDelegate?.registerButtonClicked(indexPath)
}
@IBAction func overviewButtonClicked(_ sender: Any) {
overViewDelegate?.overviewButtonClicked(indexPath)
}
@IBAction func evaluateButtonClicked(_ sender: Any) {
overViewDelegate?.evaluateButtonClicked(indexPath)
}
使用 :-
let cell = tableView.cellForRow(at: indexPath)
这篇关于iOS如何知道哪个按钮被按下了带有自定义委托的CollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!