【问题标题】:iOS how know which button is pressed CollectionView with custom delegateiOS如何知道使用自定义委托按下哪个按钮CollectionView
【发布时间】:2018-02-20 09:31:55
【问题描述】:

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

如有任何帮助,将不胜感激。

【问题讨论】:

    标签: ios swift delegates uicollectionviewcell


    【解决方案1】:

    在 Cell 类中传递 indexPath 值,在 Button Click 函数中返回 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 或任何其他解决方法
    【解决方案2】:

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

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

    【讨论】:

    • 这个方法很好用,谢谢!我怎样才能实现它,以便我可以为除所选单元之外的所有单元做一些事情?比如,隐藏其他单元格中的所有标签而不是按下按钮?
    【解决方案3】:

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

    因此,每次按下按钮时,您都有数据模型的实例

    【讨论】:

      【解决方案4】:

      您可以通过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】:
        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()
        
        }
        

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            【解决方案7】:

            您可以通过为 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()
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2022-08-18
              • 2023-03-09
              • 1970-01-01
              • 2011-04-11
              • 2021-10-08
              • 2021-06-25
              相关资源
              最近更新 更多