CoderJ13
CoderJ13

Reputation: 117

UITableView add row from other Viewcontroller

I have two view controllers one with the 3 tableviews and other controller I have a uitextfield and the text entered in the text field I want to add it to one of the tableview called ScheduleTableView in the other view controller. Here is my code but I get the error unexpectedly found nil while unwrapping an optional value at vc.ScheduleTableView.beginUpdates()

@IBAction func addButtonTapped(_ sender: YTRoundedButton) {
   self.performSegue(withIdentifier: "secondvc", sender: self)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier  == "secondvc" {
        print(TitleTextField.text!)

        let vc = segue.destination as! GrowthMainViewController

        vc.ScheduleArray.append(TitleTextField.text!)
        let indexPath = IndexPath(row: vc.ScheduleArray.count - 1, section: 0)
        vc.ScheduleTableView.beginUpdates()
        vc.ScheduleTableView.insertRows(at: [indexPath], with: .automatic)
        vc.ScheduleTableView.reloadData()
        vc.ScheduleTableView.endUpdates()



        TitleTextField.text = ""
        view.endEditing(true)

  }     
}

Upvotes: 1

Views: 450

Answers (3)

Casey West
Casey West

Reputation: 576

The resolution for this was changing where the array was declared (singleton is fine for this), and removing the unnecessary inserting, updating, reloading, etc.

The numRowsInSection method then calls the scheduleArray.count to show all corresponding data.

Before:

@IBAction func addButtonTapped(_ sender: YTRoundedButton) {
   self.performSegue(withIdentifier: "secondvc", sender: self)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier  == "secondvc" {
        print(TitleTextField.text!)

        let vc = segue.destination as! GrowthMainViewController

        vc.ScheduleArray.append(TitleTextField.text!)
        let indexPath = IndexPath(row: vc.ScheduleArray.count - 1, section: 0)
        vc.ScheduleTableView.beginUpdates()
        vc.ScheduleTableView.insertRows(at: [indexPath], with: .automatic)
        vc.ScheduleTableView.reloadData()
        vc.ScheduleTableView.endUpdates()



        TitleTextField.text = ""
        view.endEditing(true)

  }     
}

After :

@IBAction func addButtonTapped(_ sender: YTRoundedButton) {
   self.performSegue(withIdentifier: "secondvc", sender: self)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier  == "secondvc" {

        guard let text = TitleTextField.text else { return }
        scheduleArray.append(text)
        TitleTextField.text = ""
        view.endEditing(true)

  }     
}

Upvotes: 1

Leonardo Saragiotto
Leonardo Saragiotto

Reputation: 81

The problem is that you trying to update a view controller inside of prepare function. Inside that function, the view is instantiated but it's Outlets are not connected yet.

To solve that issue, follow these steps:

On this method you should first update your model:

@IBAction func addButtonTapped(_ sender: YTRoundedButton) {
   // update your model here

   self.performSegue(withIdentifier: "secondvc", sender: self)    
}

In your destination view controller, you should handle this model change and reload the table view's data.

override func viewDidLoad() {
    self.tableView.reloadData()
}

Upvotes: 0

Josh O'Connor
Josh O'Connor

Reputation: 4962

vc.ScheduleArray.count - 1 is probably a negative indexpath

try this

if (vc.ScheduleArray.count - 1 >= 0){
    vc.ScheduleTableView.insertRows(at: [indexPath], with: .automatic)
}

Upvotes: 0

Related Questions