您好我正在使用tableview单元格中的自定义操作开发应用程序。
当用户滑动单元格时。将有2个动作:删除等等。我想要更多动作来呈现一个新的viewController并将对象数据传递给新的viewController。我们如何实现这一目标?
我尝试过使用此
viewDidLoad
中的
if let split = self.splitViewController {
let controllers = split.viewControllers
self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
}
和editActionsForRowAt
方法
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let moreRowAction = UITableViewRowAction(style: .normal, title: "More", handler:{action, indexpath in
if let indexPath = self.tableView.indexPathForSelectedRow {
let object = self.fetchedResultsController.object(at: indexPath)
let controller = self.detailViewController!
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
self.present(controller, animated: true, completion: nil)
}
// self.performSegue(withIdentifier: "showDetail", sender: self)
});
moreRowAction.backgroundColor = UIColor(red: 0.298, green: 0.851, blue: 0.3922, alpha: 1.0);
let deleteRowAction = UITableViewRowAction(style: .destructive, title: "Delete", handler:{action, indexpath in
self.deleteTapped(indexPath: indexPath as NSIndexPath)
});
return [deleteRowAction, moreRowAction];
}
删除功能可以正常工作,但更多操作不起作用。非常感谢帮助!感谢
我的prepareforseuge
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let object = self.fetchedResultsController.object(at: indexPath)
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
答案 0 :(得分:2)
您可以通过sender
参数将对象传递给segue:
let moreRowAction = UITableViewRowAction(style: .normal, title: "More", handler:{action, indexpath in
let object = self.fetchedResultsController.object(at: indexPath)
self.performSegue(withIdentifier: "showDetail", sender: object)
});
然后在prepare(for segue: sender:)
-
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
guard let navVC = segue.destination as? UINavigationController,
let destVC = navVC.viewControllers.first as? DetailViewController else {
return
}
var object = sender as? Event
if object == nil {
if let indexPath = self.tableView.indexPathForSelectedRow {
object = self.fetchedResultsController.object(at: indexPath)
}
}
destVC.detailItem = object
}
}
Master-Detail模板项目使用导航控制器作为详细目的地,因此prepare(for segue)
功能需要访问导航控制器的第一个视图控制器。
此外,由于您使用相同的segue进行行点击和“更多”操作,我的代码会检查两种可能性 - 详细信息项可能位于发件人或所选行中