iOS如何知道哪个按钮按下了CollectionView与自定义委托

时间:2018-02-20 09:31:56

标签: ios swift delegates uicollectionviewcell

所以我在集合视图中有一个单元格,里面有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 个答案:

答案 0 :(得分:6)

在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)

答案 1 :(得分:1)

最好的方法是在协议方法中发回单元格.. 例如

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

}

并点击按钮

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

并在实现委托方法时使用viewcontroller

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

答案 2 :(得分:0)

在您的单元格中放置一个变量,并将会话数据模型实例保存在cellForRowAt中。然后更新您的委托方法并为每个方法添加会话参数。

所以你按下每个按钮的数据模型实例

答案 3 :(得分:0)

您可以使用PtrToStringAuto

中的按钮标记进行查看
cellForItemAt:

答案 4 :(得分: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()

}

答案 5 :(得分:-1)

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

答案 6 :(得分:-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()
}