在 swift 中调用 accessoryButtonTappedForRowWithIndexPath 的格式是什么
What is the format to call accessoryButtonTappedForRowWithIndexPath in swift
在我的 iOS table 视图中 我有 TableViewDelegate
:
所需的功能
override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
savedRow = indexPath.row
self.performSegueWithIdentifier("SubcategorySegue", sender: self)
}
我首先通过执行以下命令获得 indexPath
:
@IBAction func showSubcategoryEditor(sender: UIButton) {
let switchFrameOrigin = sender.frame.origin
if let indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(switchFrameOrigin) {
tableView.delegate.accessoryButtonTappedForRowWithIndexPath!(indexPath)
}
}
这会导致错误,指示无法识别委托。
我应该使用:tableView.accessoryButtonTappedForRowWithIndexPath(indexPath)
吗?
Apple 论坛上提供了答案:
Within the UITableViewController
subclass, which implements the
delegate protocol, you can call the delegate method like this:
tableView(tableView, accessoryButtonTappedForRowWithIndexPath:
indexPath)
当您使用系统提供的配件按钮,点击按钮时,系统会为您调用accessoryButtonTappedForRowWithIndexPath,并传入正确的indexPath。当您自己调用该方法时,您必须传入从 indexPathForRowAtPoint 获取的 indexPath(但您需要将该点转换为 table 视图的坐标系,如我在下面的代码中所示)。因此,由于您在按钮的操作方法中已经有了 indexPath,因此无需调用 accessoryButtonTappedForRowWithIndexPath,这只是一个额外的步骤,不会比您在按钮的操作方法中做的更多。你只需要这样做,
@IBAction func showSubcategoryEditor(sender: UIButton) {
let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
if let indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(hitPoint) {
savedRow = indexPath.row
self.performSegueWithIdentifier("SubcategorySegue", sender: indexPath)
}
}
另请注意,在 performSegue:sender: 中,我将 indexPath 作为 sender 参数传递。然后,如果您需要将信息传递给目标视图控制器,则可以在 prepareForSegue:sender: 中使用它来获取行。
如果将 segue 从按钮连接到下一个控制器,则甚至不需要按钮操作方法;一切都可以在 prepareForSegue:sender: 中完成。发件人参数将是按钮,所以你可以这样做,
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let button = sender as UIButton
let controller = segue.destinationViewController as DetailViewController // substitute the class of your destination view controller
let hitPoint = button.convertPoint(CGPointZero, toView: self.tableView)
if let indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(hitPoint) {
// pass the index path to controller, or get data from your array based on the indexPath, and send that to controller
}
}
在我的 iOS table 视图中 我有 TableViewDelegate
:
override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
savedRow = indexPath.row
self.performSegueWithIdentifier("SubcategorySegue", sender: self)
}
我首先通过执行以下命令获得 indexPath
:
@IBAction func showSubcategoryEditor(sender: UIButton) {
let switchFrameOrigin = sender.frame.origin
if let indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(switchFrameOrigin) {
tableView.delegate.accessoryButtonTappedForRowWithIndexPath!(indexPath)
}
}
这会导致错误,指示无法识别委托。
我应该使用:tableView.accessoryButtonTappedForRowWithIndexPath(indexPath)
吗?
Apple 论坛上提供了答案:
Within the
UITableViewController
subclass, which implements the delegate protocol, you can call the delegate method like this:
tableView(tableView, accessoryButtonTappedForRowWithIndexPath:
indexPath)
当您使用系统提供的配件按钮,点击按钮时,系统会为您调用accessoryButtonTappedForRowWithIndexPath,并传入正确的indexPath。当您自己调用该方法时,您必须传入从 indexPathForRowAtPoint 获取的 indexPath(但您需要将该点转换为 table 视图的坐标系,如我在下面的代码中所示)。因此,由于您在按钮的操作方法中已经有了 indexPath,因此无需调用 accessoryButtonTappedForRowWithIndexPath,这只是一个额外的步骤,不会比您在按钮的操作方法中做的更多。你只需要这样做,
@IBAction func showSubcategoryEditor(sender: UIButton) {
let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
if let indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(hitPoint) {
savedRow = indexPath.row
self.performSegueWithIdentifier("SubcategorySegue", sender: indexPath)
}
}
另请注意,在 performSegue:sender: 中,我将 indexPath 作为 sender 参数传递。然后,如果您需要将信息传递给目标视图控制器,则可以在 prepareForSegue:sender: 中使用它来获取行。
如果将 segue 从按钮连接到下一个控制器,则甚至不需要按钮操作方法;一切都可以在 prepareForSegue:sender: 中完成。发件人参数将是按钮,所以你可以这样做,
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let button = sender as UIButton
let controller = segue.destinationViewController as DetailViewController // substitute the class of your destination view controller
let hitPoint = button.convertPoint(CGPointZero, toView: self.tableView)
if let indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(hitPoint) {
// pass the index path to controller, or get data from your array based on the indexPath, and send that to controller
}
}