如何通过editActionsForRowAt将数据传递给Viewcontroller?

时间:2018-12-21 09:59:24

标签: swift uitableview

我想将数据从我的tableview传递到另一个viewcontroller。 我不知道该怎么做,但是我已经尝试过了,但是它不起作用:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in

        let eventDB = self.events[indexPath.row]
        self.events.remove(at: indexPath.row)
        self.tableViewHome.deleteRows(at: [indexPath], with: .automatic)
        self.removeFromDB(event: eventDB)
        print(self.tableViewHome)
    }

    let edit = UITableViewRowAction(style: .default, title: "Edit") { (action, indexPath) in

        let EC = EditEventController()
        EC.dateTextfield.text = self.events[indexPath.row].dateTitle
        EC.titleTextfield.text = self.events[indexPath.row].eventTitle
        EC.repeatTextfield.text = self.events[indexPath.row].repeatTime

        self.navigationController?.pushViewController(EC, animated: true)

        //self.performSegue(withIdentifier: "goToEditEvent", sender: self)

    }

    edit.backgroundColor = UIColor.lightGray

    return [delete, edit]

}

1 个答案:

答案 0 :(得分:0)

EditEventController中的IBOutlet一直为零,直到您实际进行pushViewController为止,这就是为什么看到它们没有设置的原因。

我建议做的是向event中添加一个originalEvent(或EditEventController)属性,然后在EditEventController的{​​{1}}函数中添加,然后设置网点的文本属性。

也就是说,在您的viewWillAppear中:

EditEventController

然后,在您的var originalEvent : Event? // or whatever you've named this structure/object override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) guard let originalEvent = originalEvent else { return } self.dateTextfield.text = originalEvent.dateTitle self.titleTextfield.text = originalEvent.eventTitle self.repeatTextfield.text = originalEvent.repeatTime } 中:

UITableViewRowAction

还注意到我使let edit = UITableViewRowAction(style: .default, title: "Edit") { (action, indexPath) in let eec = EditEventController() eec.originalEvent = self.events[indexPath.row] self.navigationController?.pushViewController(eec, animated: true) } 实例全部小写了吗? Starting with a lowercase letter is the standard way to start naming a variable in Swift

我希望这会有所帮助!