swift中tableView中按下了哪个单元格button?

我已经阅读了类似这样的问题,但这些代码没有为我工作,所以请帮助:我有一个表格视图与每个单元格中的一些button – 此表视图是用户的因素,我添加button到该tableview – 当用户有付钱的button隐藏在该单元格中的因素,在另一个单元格中的另一个因素,当用户没有支付薪酬的钱不隐藏 – 这就是我想要检测哪些单元格button按下? 我知道,在表视图中,我们可以检测到哪个单元格按下但是这是不同的,因为用户只要按下该单元格中的button不按下所有的单元格

由于我的代码太长,我只是把表视图代码在这里

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "factorCell", for: indexPath) as! FactorCell cell.factorTitle.text = factorTitle[indexPath.row] cell.factorCode.text = factorCodes[indexPath.row] cell.factorDate.text = factorDate[indexPath.row] cell.factorHour.text = factorHour[indexPath.row] cell.factorPrice.text = factorPrice[indexPath.row] cell.factorCondition.text = factorConditions[indexPath.row] cell.factorBill.tag = indexPath.row cell.factorBill.addTarget(self,action:#selector(factorViewController.Bill), for: UIControlEvents.touchUpInside) cell.factorDetail.tag = indexPath.row cell.factorDetail.addTarget(self,action:#selector(factorViewController.FactorDetail), for: .touchUpInside) if factorConditions[indexPath.row] == "پرداخت شده" { cell.factorBill.isHidden = true } else { cell.factorCondition.textColor = UIColor.red cell.factorBill.isHidden = false } return cell } 

这是我想要的function:当用户点击支付他的因素,并按下button – 移动到另一个视图控制器,在该视图控制器用户可以在这里看到这些数据视图控制器 –

**非常感谢如果你不明白我想要做什么告诉我,我会解释更多**

  Here is update just leave it, i explained it more simple, create a button action in your cell class,then Writre the code:- > //Cell class protocol CellDelegate: class { func didTapCell(index: IndexPath) } var delegateCell:CellDelegate? var indexPath:IndexPath? @IBAction func buttonCellClick(_ sender: Any) { delegateCell?.didTapCell(index: indexPath!) } > //In ViewController cell.delegateCell = self cell.indexPath = indexPath func didTapCell(index: IndexPath) { print("PRESS BUTTON >> \(index.row)") } 

在自定义单元类中创buildIBActionbutton,然后声明一个将在button单击时调用的块。

 var ButtonHandler:(()-> Void)! @IBAction func ButtonClicked(_ sender: Any) { self.ButtonHandler() } 

然后在你的tableview类里面的cell方法中,

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = playlistTableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell cell.ButtonHandler = {()-> Void in // your code } return cell } 

您在cellForRowAt中设置因子button的标记,以便您可以轻松从factorViewController.Bill获取factorViewController.Bill ,并从该indexpath获取整个单元格,并且可以执行任何所需的操作。

你可以像细胞一样,

  let cell = tableView.cellForRowAtIndexPath(indexPath) 

你可以参考这个post来知道如何从button点击获取indexpath!

当您按下factorBillbutton时,您的Bill方法将会被调用。因此,您可以通过sender.tag轻松地区分Bill方法中的sender.tag 。您已经将标记设置为cellForRowAt