iOS如何知道哪个按钮被按下,带有自定义代理的CollectionView。

3

我有一个带有3个按钮的集合视图单元格。为了使用这些按钮触发代码,我实现了一个自定义代理。现在代码已经被触发,但我不知道代码是从哪个单元格触发的。我该如何最好地实现这个?这里是我的一些代码。 协议:

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!
}

单元格:

import UIKit

导入 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()
}

任何帮助都将不胜感激。
7个回答

7
在Cell类中传递indexPath值,通过按钮点击函数返回indexPath值。
协议:
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)

let cell = tableView.cellForRow(at: indexPath) 是什么意思?您如何在单元格控制器中重写按钮委托?这对CollectionView是否有效,或者还有其他解决方法? - iSrinivasan27

3

最好的方法是在协议方法中返回单元格。例如:

protocol OverViewDelegate {
  func registerButtonClicked(cell: SessionCollectionViewCell)
  func evaluateButtonClicked(cell: SessionCollectionViewCell)
  func overviewButtonClicked(cell : SessionCollectionViewCell)

}

当点击按钮时

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

在你的视图控制器中实现委托方法时,使用以下代码:
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
    }
 }

这个方法非常好用,谢谢!我该如何实现它,以便我可以为除选定单元格之外的所有单元格执行某些操作?比如,在按下按钮的单元格之外隐藏所有标签? - oğuz

0
在单元格中放置一个变量,并在cellForRowAt中保存您的会话数据模型实例。然后更新您的委托方法,并为每个方法添加一个会话参数。
这样,您就可以在每次按钮按下时获得您的数据模型实例。

0

您可以在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!
}

0
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()

}

-1
cellForItemAt: 方法中,您可以指定 tag 值,当按钮被点击时,您可以检查 sender.tag 来确定哪个按钮被点击了。

-2

你可以通过在 cellForItemAt: 中给所有按钮打标签来实现它,如下所示

cell.sessionRegisterButton.tag = indexPath.item
cell.sessionOverviewButton.tag = indexPath.item
cell.sessionEvaluateButton.tag = indexPath.item

通过以下方式获取索引:

@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()
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接