【问题标题】:index of button in custom cell自定义单元格中的按钮索引
【发布时间】:2019-05-22 09:00:54
【问题描述】:

我创建了一个包含按钮的自定义单元格,我需要创建从这个按钮到其他 VC 的 segue,但首先,我想用那个 segue 推送一个对象。

我已经尝试使用 cell.button.tag,但没有成功。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == "showMap" {
            let mapVC = segue.destination as! MapViewController
            //guard let indexPath = tableView.indexPathForSelectedRow else { return }
            mapVC.place = places[] // <- "here I need index of button in cell"
        }
    }

【问题讨论】:

标签: ios swift uitableview


【解决方案1】:

不使用segue,而是通过UITableViewCell 中的closure 以编程方式处理navigation

class CustomCell: UITableViewCell {
    var buttonTapHandler: (()->())?

    @IBAction func onTapButton(_ sender: UIButton) {
        self.buttonTapHandler?()
    }
}

在上面的代码中,我创建了一个buttonTapHandler - 一个closure,只要点击cell 中的button,就会调用它。

现在,在cellForRowAt 方法中,当您dequeue 单元格时,设置buttonTapHandlerCustomCell

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
    cell.buttonTapHandler = {[weak self] in
        if let mapVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MapViewController") as? MapViewController {
            mapVC.place = places[indexPath.row]
            self?.navigationController?.pushViewController(mapVC, animated: true)
        }
    }
    return cell
}

在上面的代码中,buttonTapHandler 在被调用时将push 一个新的MapViewController 实例以及基于indexPath 的相关place

【讨论】:

    【解决方案2】:

    如果您不想在 didSelectRowAt 方法中执行代码,我认为另一种好方法是创建自定义单元格的委托。请看下面的代码

    // This is my custom cell class
    class MyCustomCell: UITableViewCell {
    
        // The button inside your cell
        @IBOutlet weak var actionButton: UIButton!
        var delegate: MyCustomCellDelegate?
    
        @IBAction func myDelegateAction(_ sender: UIButton) {
            delegate?.myCustomAction(sender: sender, index: sender.tag)
        }
    
        // Here you can set the tag value of the button to know
        // which button was tapped
        func configure(index: IndexPath){
           actionButton.tag = index.row
        }
    
    }
    
    protocol MyCustomCellDelegate {
        func myDelegateAction(sender: UIButton, index: Int)
    }
    

    委派您使用自定义单元格的 ViewController。

    class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "MyCellIdentifier", for: indexPath) as! MyCustomCell
    
          cell.configure(index: indexPath)
          cell.delegate = self
    
          return cell
        }
    }
    

    最后自定义扩展自定义单元格委托的方法

    extension MyViewController: MyCustomCellDelegate {
    
        func myDelegateAction(sender: UIButton, index: Int) {
            // Do your staff here
        }
    }
    

    希望对你有所帮助。

    【讨论】:

      【解决方案3】:

      在自定义单元格中:

      import UIKit
      
      protocol CustomCellDelegate: class {
          func btnPressed(of cell: CustomCell?)
      }
      
      class CustomCell: UITableViewCell {
      
          weak var delegate: CustomCellDelegate?
      
          @IBAction func btnTapped(_ sender: UIButton) {
              delegate?.btnPressed(of: self)
          }
      
      }
      

      在视图控制器中:

          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              let cell: CustomCell = tableView.dequeueReusableCell(for: indexPath)
              cell.delegate = self
      
              return cell
          }
      
          extension ViewController: CustomCellDelegate {
      
          func btnPressed(of cell: CustomCell?) {
              if let cell = cell, let indexPath = tableView.indexPath(for: cell) {
                  // Your stuff here
              }
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-07
        • 1970-01-01
        • 1970-01-01
        • 2014-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多