0

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)

  • Add the console error you are getting for us to be able to help you better – Dante Puglisi Sep 14 '17 at 19:00
  • just a tip, when defining variables use lower camel case to prevent confusion with types. – Pranav Kasetti Sep 14 '17 at 19:13
  • Thanks for the advice. Very new to coding. – Leon Vladimirov Sep 14 '17 at 19:14
  • possible duplicate of [How to pass prepareForSegue: an object](https://stackoverflow.com/questions/7864371/how-to-pass-prepareforsegue-an-objecthttps://stackoverflow.com/questions/7864371/how-to-pass-prepareforsegue-an-object). `prepareForSegue` will give a reference to the destination view controller. At this point you can set the data source for your table view. – Chris Sep 14 '17 at 19:18
  • could you please post your cellforrow at method? You're probably accessing a nil object. – Pranav Kasetti Sep 14 '17 at 19:21

1 Answers1

0

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)
maxkoriakin
  • 337
  • 3
  • 7