【发布时间】:2015-12-02 03:32:54
【问题描述】:
我是 iOS 编程的新手,我一直在寻找如何做到这一点,但似乎找不到我想要的东西。我认为这将是一项相当容易的任务,我相信如果有人能启发我的话。
当您在表格视图上向左滑动时,我可以执行一些自定义操作。其中一项操作是对所选项目的简单“编辑”。我要做的就是显示另一个视图控制器,但将一些数据传递给该视图控制器,就像它通常在 prepareForSegue(...) 中所做的那样。
这就是我所拥有的。请参阅标记为“//选项 2”的部分...
// Override to provide additional edit actions when the users swipes the row to the left.
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let deleteAction = UITableViewRowAction(
style: UITableViewRowActionStyle.Normal, title: "Delete", handler: deleteHandler);
deleteAction.backgroundColor = UIColor.redColor()
let editAction = UITableViewRowAction(
style: UITableViewRowActionStyle.Normal, title: "Edit", handler: editHandler);
editAction.backgroundColor = UIColor.grayColor()
return [deleteAction, editAction]
}
// Handles the delete action when the users swipes the row to the left.
func editHandler(action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void {
// Option 1
//performSegueWithIdentifier("EditItem", sender: nil)
//Option 2 - This does not work.
let myViewController = ItemViewController()
let selectedItem = self.items[indexPath.row] // I have an array of 'items'
myViewController.item = selectedItem
self.presentViewController(myViewController, animated: true, completion: nil)
}
// Handles the delete action when the users swipes the row to the left.
func deleteHandler(action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void {
self.games.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
如何在“editHandler”方法中将数据传递给我的视图控制器?我可以使用 segue 整天显示视图控制器,但我希望它与表中的选定项目一起加载,以便用户可以修改它。
谢谢!
【问题讨论】:
标签: ios swift2 tableview viewcontroller