可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
Pass indexPath value in Cell class return back indexPath on Button Click function
protocol:-
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!
}
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)
}
get cell using :-
let cell = tableView.cellForRow(at: indexPath)
回答2:
The best way to do is in protocol method send back the cell..
for example
protocol OverViewDelegate {
func registerButtonClicked(cell: SessionCollectionViewCell)
func evaluateButtonClicked(cell: SessionCollectionViewCell)
func overviewButtonClicked(cell : SessionCollectionViewCell)
}
and on button click
@IBAction func overviewButtonClicked(_ sender: Any) {
overViewDelegate?.overviewButtonClicked(cell: self)
}
and on you viewcontroller when delegate method implemented
func overviewButtonClicked(cell: SessionCollectionViewCell) {
// here you get the cell and all the properties you want to access of that cell and also
if let indexPath = self.tableView.indexPath(for: cell) {
// do what you want to do
}
}
回答3:
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
回答4:
You can check with button tag in cellForItemAt:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "sessionCell", for: indexPath) as? SessionCollectionViewCell
cell.YOUR_BUTTON.tag = indexPath.item
return cell!
}
回答5:
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.sessionRegisterButton.tag = indexPath.row // --- add this
..........
cell?.sessionImage.image = #imageLiteral(resourceName: "carControl")
cell?.sessionNameLabel.text = session.name
cell?.sessionLocationLabel.text = session.location
cell?.overViewDelegate = self
return cell!
}
@IBAction func registerButtonClicked(_ sender: Any) {
let cellIndex = sender.tag // this will be the cell index
overViewDelegate?.registerButtonClicked()
}
回答6:
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.
回答7:
You can achieve it by giving tags to all button in cellForItemAt: as bellow
cell.sessionRegisterButton.tag = indexPath.item
cell.sessionOverviewButton.tag = indexPath.item
cell.sessionEvaluateButton.tag = indexPath.item
And get index back by :
@IBAction func registerButtonClicked(_ sender: Any) {
let senderButton : UIButton = sender as! UIButton
let index : Int = senderButton.tag
overViewDelegate?.registerButtonClicked()
}
@IBAction func overviewButtonClicked(_ sender: Any) {
let senderButton : UIButton = sender as! UIButton
let index : Int = senderButton.tag
overViewDelegate?.overviewButtonClicked()
}
@IBAction func evaluateButtonClicked(_ sender: Any) {
let senderButton : UIButton = sender as! UIButton
let index : Int = senderButton.tag
overViewDelegate?.evaluateButtonClicked()
}