iOS how know which button is pressed CollectionVie

2019-02-19 04:44发布

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.

7条回答
萌系小妹纸
2楼-- · 2019-02-19 04:48

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)
查看更多
时光不老,我们不散
3楼-- · 2019-02-19 04:52

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
    }
 }
查看更多
一纸荒年 Trace。
4楼-- · 2019-02-19 04:52

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楼-- · 2019-02-19 04:53

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

查看更多
ら.Afraid
6楼-- · 2019-02-19 04:55
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()

}
查看更多
冷血范
7楼-- · 2019-02-19 05:02

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()
}
查看更多
登录 后发表回答