iOS如何知道哪个按钮被按下了带有自定义委托的CollectionView

iOS how know which button is pressed CollectionView with custom delegate(iOS如何知道哪个按钮被按下了带有自定义委托的CollectionView)
本文介绍了iOS如何知道哪个按钮被按下了带有自定义委托的CollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在一个集合视图中有一个单元格,里面有 3 个按钮.为了使用这些按钮触发代码,我实现了一个自定义委托.现在代码正在被触发,但我不知道代码是从哪个单元格触发的.我怎样才能最好地实现这一点?这是我的一些代码.协议:

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:

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

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.

推荐答案

在Cell类中传递indexPath值在Button Click函数中返回indexPath

Pass indexPath value in Cell class return back indexPath on Button Click function

协议:-

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)

这篇关于iOS如何知道哪个按钮被按下了带有自定义委托的CollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)