【问题标题】:How can I tell to my prepare method on which cell a button has been pressed? [duplicate]如何告诉我的准备方法在哪个单元格上按下了按钮? [复制]
【发布时间】:2018-06-22 03:09:39
【问题描述】:

我在我的自定义 tableViewCell 类中添加了一个按钮 @IBOutlet weak var cellButton: UIButton!,并在我的 tableView 控制器中添加了按钮的操作

 @IBAction func cellButtonTap(_ sender: UIButton) {

      performSegue(withIdentifier: "goToMap" , sender: self)

    }

我需要做的是将数据传递给另一个视图控制器,但重要的是要知道在哪个单元格上按下了按钮,所以我如何告诉我的方法准备

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

        if segue.identifier == "goToMap"...//HERE I DON'T KNOW HOW TO DO

}

在哪个单元格上按下了按钮?我是初学者,找了两天的解决方法还没找到

【问题讨论】:

  • 只需设置单元格的标签,当您按下单元格时,检查单元格的标签以了解正在按下哪个单元格
  • 在您的自定义单元格中使用协议。这是一个例子:medium.com/@aapierce0/…

标签: ios swift uitableview swift4


【解决方案1】:

您可以使用“回调”闭包来实现...

class MyTableViewCell: UITableViewCell {

    var didButtonTapAction : (()->())?

    @IBAction func cellButtonTap(_ sender: UIButton) {
        // call back closure
        didButtonTapAction()?
    }

}

在您的表格视图控制器中,添加一个类级变量:

var tappedIndexPath: IndexPath?

然后您的表格视图单元格设置变为:

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

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

    // set labels, colors, etc in your cell

    // set a "call back" closure
    cell.didButtonTapAction = {
        () in
        print("Button was tapped in cell at:", indexPath)

        // you now have the indexPath of the cell containing the
        // button that was tapped, so
        // call performSegue() or do something else...

        self.tappedIndexPath = indexPath
        performSegue(withIdentifier: "goToMap" , sender: self)
    }

    return cell
}

并且,在准备中:

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

    if segue.identifier == "goToMap" {
         // do what you need based on the row / section
         // of the cell that had the button that was tapped, such as:

        if let vc = segue.destination as? MyMapViewController {
              vc.myData = self.dataArray[self.tappedIndexPath.row]
        }

    }

}

【讨论】:

  • 啊好吧,所以在我的准备中,我只需要写 if segue.identifier == "goToMap"{ } 并且它已经考虑到哪个单元格是按钮?
  • 我编辑了答案...应该可以帮助您。
  • 很好的答案,谢谢
【解决方案2】:
  1. 您确定需要按钮吗?通常它是通过表格视图选择委托发生的。

  2. 如果您需要一个按钮,您需要提供一种方法来识别按下的按钮。如果您要呈现简单的一维数组,最简单的方法是将按钮的tag 设置为索引

喜欢:

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "<id>", for: indexPath) as? Cell else { return UITableViewCell() }
        cell.button.tag = indexPath.row
        //...
        return cell
    }

那么最好用按钮作为发送者来触发segue:

 @IBAction func cellButtonTap(_ sender: UIButton) {
      performSegue(withIdentifier: "goToMap" , sender: sender)
 }

你可以得到你的数据:

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "goToMap", let index = (sender as? UIView)?.tag {

        }

}

【讨论】:

  • 使用标签来表示索引路径是一个糟糕的选择。如果表格视图允许删除、插入或移动行,则标记将是错误的。
  • 正如我所说,它适用于最简单的情况。任何“单元格中的按钮”解决方案都可能存在类似的限制,例如块等。
  • 不,正确的解决方案没有限制。
  • 这是一个哲学问题。当以前的合理解决方案停止工作时,您总是可以想象边缘情况。但通常优秀的开发人员会估计他的解决方案将如何被使用并使用最简单的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-21
  • 2017-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-12
相关资源
最近更新 更多