0

So I am trying to pass the arrays from a previous view controller called configNames to populate my table view controller (basically a scoreboard)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destVC: scoreboard = segue.destination as! scoreboard
    destVC.namesTArray = nameArray
    destVC.scoresTArray = scoreArray
}

namesTArray/scoresTarray is the array created in the tableview (scoreboard) and nameArray/scoreArray are created and the values are added in the view controller.

I tried passing the arrays through segues, however, the values in the arrays were not passing and are causing a crash when I am trying to call the data from the variables to be displayed.

Thanks for any help.

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
BigNeil
  • 13
  • 5
  • try `if let destVC: scoreboard = segue.destination as? scoreboard{}` – Reinier Melian Jan 08 '18 at 14:50
  • Provide more code and crash log also. – Iraklii Jan 08 '18 at 14:53
  • Try printing the values of `destVC` and the arrays in your `prepare` method and also the values of `self` and the arrays inside `scoreboard`'s `viewDidLoad`. – Phillip Mills Jan 08 '18 at 14:55
  • 1
    "causing a crash": What crash? What is logged? Doesn't seems to be an issue with your code, except that you should name your classes starting with an uppercase... – Larme Jan 08 '18 at 15:00
  • it is stating that I am getting a 'Fatal Error: Index out of range' on the line which I am trying to set the data on the table cell – BigNeil Jan 08 '18 at 15:05
  • Well, then do you mind showing us the code for the setting of the UITableView? – Larme Jan 08 '18 at 16:20

2 Answers2

0

You should give segue from your one controller to another controller.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "SegueName" {
        let detailVC = segue.destination as! scoreboard
        destVC.namesTArray = nameArray
        destVC.scoresTArray = scoreArray
    }
}
Nikhil Manapure
  • 3,748
  • 2
  • 30
  • 55
Khawar Islam
  • 2,556
  • 2
  • 34
  • 56
0

Try it in this way

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destVC = segue.destination as? scoreboard {
        destVC.namesTArray = nameArray
        destVC.scoresTArray = scoreArray
    }
}

Now you are not force unwraping the variable

And tableview automatically reloads itself after viewDidLoad so tableview should get populated without any other effort.

And if you ever face any crash it would be best that you provide line number and if possible stack trace too, then solving issue will be easier.

And do change scoreboard to ScoreBoard. This kind of naming convention is called casing.

By the description of crash 'Fatal Error: Index out of range' you have given in comments, if feels that you are passing wrong count in numberOfRowInSection.

Make sure that nameArray and scoreArray have same count, and that you put nameArray.count in numberOfRowInSection.

Nikhil Manapure
  • 3,748
  • 2
  • 30
  • 55