【问题标题】:Having multiple buttons in custom UITableViewCell在自定义 UITableViewCell 中有多个按钮
【发布时间】:2019-10-24 07:32:13
【问题描述】:

假设我在自定义 UITableViewCell 中有 10 个按钮。如何识别点击了哪个按钮并在 ViewController 中执行相应的操作来保存单元格?我正在迅速寻找一个乐观的解决方案。谢谢

【问题讨论】:

  • 使用tag 概念来识别每个对象,谷歌它你得到答案
  • 为自定义按钮设置标签,在按钮的目标动作中检查标签值
  • 使用闭包将点击的按钮发送回控制器。

标签: ios swift xcode


【解决方案1】:

//把这段代码放到你的UITableViewDataSource中:cellForRowAt

//单元格必须包含这些按钮,你需要在这些按钮上添加do addTarget

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "currentTableViewCell", for: indexPath)
        as! CurrentTableViewCell

    cell.btn1.tag = 1
    cell.btn1.addTarget(self, action: #selector(buttonPressed(sender:)), for: .touchUpInside)

    cell.btn2.tag = 2
    cell.btn2.addTarget(self, action: #selector(buttonPressed(sender:)), for: .touchUpInside)

    cell.btn3.tag = 3
    cell.btn3.addTarget(self, action: #selector(buttonPressed(sender:)), for: .touchUpInside)

    return cell
}

@objc func buttonPressed(sender: UIButton) {

    let convertedPointInTable = sender.convert(CGPoint.zero, to:self.currentTableView)
    let retriveIndexPath = self.currentTableView.indexPathForRow(at: convertedPointInTable)
    print("In which cell \(retriveIndexPath!.row), which button pressed \(sender.tag)")
}

【讨论】:

    【解决方案2】:

    单元格代码

    final class MyCell: UITableViewCell {
        struct ConfiguringData {
            let action1: () -> Void
            let action2: () -> Void
        }
        @IBOutlet private weak var button1: UIButton!
        @IBOutlet private weak var button2: UIButton!
    
        private var didTapButton1Action: (() -> Void)?
        private var didTapButton2Action: (() -> Void)?
    
        @IBAction private func didTapButton1() {
            didTapButton1Action?()
        }
        @IBAction private func didTapButton2() {
            didTapButton2Action?()
        }
    
        func configure(with configuringData: ConfiguringData) {
            didTapButton1Action = configuringData.action1
            didTapButton2Action = configuringData.action2
        }
    }
    

    ViewController 代码

    class MyViewController: UIViewController {
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let myCell = tableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyCell
            let action1 = { print("didTapButton1") }
            let action2 = { print("didTapButton2") }
    
            myCell.configure(
                with: MyCell.ConfiguringData(action1: action1, action2: action2)
            )
    
            return myCell
        }
    }
    

    【讨论】:

      【解决方案3】:

      **有两种方法可以做到这一点 =:
      首先,您可以收集 IB 按钮插座并定义标签。
      第二个是相同的,但不是收集插座,而是转到您的故事板并单击按钮,您将在那里看到标签选项,每个按钮都给出不同的标签(假设如果您有 10 个按钮,则给出标签 1 到 10) 由于您是在自定义单元格下执行此操作,因此您可以在 UITableViewCell 类中执行上述操作,也可以在 cellForRowAt 下执行目标,两者都可以正常工作

      // Now inside your button action do this -:
      
      
      
      
      
         if sender.tag == 1{
          print("one")
          }
          if sender.tag == 2{
          print("two")
          }
                   //OR (if inside cellForRowAt)
       cell.button.addTarget(self, action:#selector(handleRegister), for: .touchUpInside)
      
      *so on...
      

      //确保IBAction中的sender type必须是UIButton

      //你也可以把它放在switch case下,只是基本的编程*

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-15
        • 2018-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多