简体   繁体   English

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

[英]iOS how know which button is pressed CollectionView with custom delegate

So I have a cells in a collectionview with 3 buttons in it. 所以我在一个集合视图中有一个单元格,里面有3个按钮。 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!
}

cell: 细胞:

import UIKit

import IBAnimatable 导入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 在Cell类中传递indexPath值返回按钮单击功能上的indexPath

protocol:- 协议:-

protocol OverViewDelegate {
func registerButtonClicked(_ indexPath : IndexPath)
func evaluateButtonClicked(_ indexPath : IndexPath)
func overviewButtonClicked(_ indexPath : IndexPath)
}

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

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 当实现委托方法时,你在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
    }
 }

Put a variable in your cell and save your session data model instance in cellForRowAt . 在您的单元格中放置一个变量,并将您的会话数据模型实例保存在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 check with button tag in cellForItemAt: 您可以使用cellForItemAt:按钮标记进行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!
}
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()

}

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

You can achieve it by giving tags to all button in cellForItemAt: as bellow 您可以通过为cellForItemAt中的所有按钮指定标记来实现它:如下所示

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何知道在 detailCalloutAccessoryView 中按下了哪个按钮 - How to know which button was pressed in a detailCalloutAccessoryView iOS如何识别按下了哪个图像(按钮)? - iOS how to identify which image (button) is pressed? 当按下TableView单元格中的按钮时,我怎么知道按下了哪一行单元格的按钮? - When the button in TableView cell pressed, how do I know the button of which row of cell pressed? 如何知道ios委托是否已发布 - How to know if ios delegate is released 知道在ios中按下facebook登录按钮的时间 - Know when facebook login button is pressed in ios 如何找到在iOS中的按钮数组上按下了哪个按钮 - How to find which button is pressed on the array of buttons in ios 如何使用Javascript确定在iOS设备上按下了哪个按钮? - How to determine which button is pressed on iOS devices using Javascript? 如何在CollectionView委托方法之外实例化自定义类UICollectionViewCell? - How to instantiate custom class UICollectionViewCell outside CollectionView delegate methods? 如果在iOS中按下按钮,如何禁用该按钮? - How to disable a button if it is pressed in iOS? 根据按下的按钮(iOS)检索自定义数据? - Retrieve custom data based on button pressed (iOS)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM