如何将委托函数转换为IBAction函数

我有一个委托函数更改UITableViewCell的节点位置:

//On cell tap, expand func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if indexPath.section == 0 { let data = firstDataSource[indexPath.row] tableView.beginUpdates() secondDataSource.append(data) firstDataSource.removeAtIndex(indexPath.row) let newIndexPath = NSIndexPath(forRow: find(secondDataSource, data)!, inSection: 1) tableView.moveRowAtIndexPath(indexPath, toIndexPath: newIndexPath) tableView.endUpdates() } else if indexPath.section == 1 { let data = secondDataSource[indexPath.row] tableView.beginUpdates() firstDataSource.append(data) secondDataSource.removeAtIndex(indexPath.row) let newIndexPath = NSIndexPath(forRow: find(firstDataSource, data)!, inSection: 0) tableView.moveRowAtIndexPath(indexPath, toIndexPath: newIndexPath) tableView.endUpdates() } } 

我希望这个动作发生在从button点击触发一个IBAction的时候,但是我不确定我如何访问在代理函数中给出的indexPath参数。 这是我当前的IBActionbutton的代码:

 @IBAction func checkOffTask(sender: UIButton) { var checkedOff = false let indexOfCell = sender.tag sender.setImage(UIImage(named: "checkbox-checked"), forState: UIControlState.Normal) self.tableView.beginUpdates() self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forItem: indexOfCell, inSection: 1)], withRowAnimation: UITableViewRowAnimation.Automatic) self.tableView.endUpdates() } 

任何想法如何让委托function的IBActionfunction工作?

好的,所以你的操作是来自一个button,那个button是在一个表视图单元格内,问题是:那个表视图单元格的indexPath是什么。

最简单的方法是:

  1. 获取表格视图中button的位置;
  2. 请求在该位置的索引path。

例如

 @IBAction func checkOffTask(sender: UIButton!) { let originInTableView = self.tableView.convertPoint(CGPointZero, fromView: sender) let indexPath = self.tableView.indexPathForRowAtPoint(originInTableView) } 

好的,这是我以前使用过的解决方法。 看起来不漂亮,但做的工作:

 UIButton *likeButton = (UIButton *)sender; UITableViewCell *cell = (UITableViewCell *)likeButton.superview.superview; NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 

只需findUICollectionView Classtypes的button的正确的超级视图。

PS – 我的Swift不是最好的。 希望你能转换这个。