简体   繁体   中英

How can I insert data into a tableView from another view controller?

I know this is probably a noob question, but how can I insert data from into a table view from another View Controller? Here is my code:

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    var needToSegue = false
    switch result {
    case .cancelled:
        print("Mail cancelled")
    case .saved:
        print("Mail saved")
    case .sent:
        needToSegue = true
        print("Mail sent")
    case .failed:
        print("Mail sent failure: \(error?.localizedDescription ?? "nil")")
    }
    controller.dismiss(animated: true) {
        if needToSegue {
            self.AllListOfViolations.addData(Time: "\(self.LocationAndTimeData.getTextDate())")
            self.performSegue(withIdentifier: "AllList", sender: self)
        }
    }
}

I need to insert time into the AllList tableView, but this doesn't work, unfortunately. Here is my addData function:

 func addData(Time: String) {

violationType.append(Time)

// Update Table Data
tableView.beginUpdates()
tableView.insertRows(at: [IndexPath(row: violationType.count-1, section: 0)], with: .automatic)
tableView.endUpdates()

  }

Please help:/

Here is the console message:

fatal error: unexpectedly found nil while unwrapping an Optional value 2017-09-14 22:09:29.011455+0300 Parkovka![504:47018] fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

you can use this.Add this code to your ViewController

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destination = segue.destination as? "YOURNAMETABLEVIEWCONTROLLER" {
        destination.time = "\(self.LocationAndTimeData.getTextDate())"
    }
}

Add property time to your tableView controller and call func in viewDidLoad in tableViewController

addData(Time: time)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM