accessoryButtonTappedForRowWithIndexPath在Swift 2中返回不需要的indexPath

时间:2015-10-19 16:43:12

标签: swift uitableview swift2 accessoryview

我在UITableViewController中调用的NSManagedObjects中填充了NSFetchRequest {/ 1}}。

除了viewDidLoad之外,myTableViewController上的所有内容均符合要求,并返回accessoryButtonTappedForRowWithIndexPath 0。

以下是我实施indexPath的方式:

accessoryButtonTappedForRowWithIndexPath

这是我的override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) { performSegueWithIdentifier("reorderSequences", sender: UIButton()) }

performSegueWithIdentifier

我的理解是使用 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "reorderSequences" { // Get a hold of the item to send to the destinationVC let button = sender as! UIButton let hitPoint = button.convertPoint(CGPointZero, fromCoordinateSpace: self.tableView) if let indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(hitPoint) { // ** Regardless of which accessoryButton I press, the indexPath // for index 0 is always the value ** print("\nhitPoint's x: \(hitPoint.x)\n hitPoint's y: \(hitPoint.y)") let selectedThingSequence = self.thingSequences[indexPath.row] print("prepareForSegue's indexPath is \(indexPath)") // set up the segue let destinationVC = segue.destinationViewController as! ReorderTableViewController destinationVC.thingSequenceToReorder = selectedThingSequence print(controller.description) } } } ,因为发件人可以让我知道发件人居住在UIButton中的indexPath。显然,事实并非如此。我错过了什么?

更新

对于每个简单的问题,都有一个简单的答案:

performSegueWithIdentifierprepareForSegue indexPath sender,如下:

override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
            performSegueWithIdentifier("reorderThings", sender: indexPath)
}

prepareForSegue中,我得到了像这样传递的indexPath:

let indexPath = sender as! NSIndexPath

如果您正在运行一堆segue,您需要小心。就我的目的而言,它有效。感谢Tom Harrington指出我正确的方向。

1 个答案:

答案 0 :(得分:1)

在这一行:

performSegueWithIdentifier("reorderSequences", sender: UIButton())

你没有发送"" UIButton。您正在创建UIButton的全新实例。它不是你点过的那个。它不在屏幕上或视图层次结构中的任何位置。它具有一切的默认值。因此,当您开始执行segue时,传入的UIButton中没有有用的信息,并且每次都会得到相同的结果。