I currently have some arrays like this that I pass from one VC to another:
From VC:
let fruitsArray = ["Oranges", "Apples", "Bannas"]
let vegetableArray = ["Potatoes", "Carrots", "Tomatoes"]
let cheeseArray = ["Mozzarella", "Cheddar", "Brie"]
let pastaArray = ["Spaghetti, linguine, fusilli"]
I pass these on to the next VC like this:
let foodMenuSegue = "ToFoodMenuSegue"
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: foodMenuSegue, sender: indexPath)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == foodMenuSegue {
let destination = segue.destination as! FoodItemTableViewController
let indexPath = sender as! IndexPath
switch indexPath.row {
case 0: destination.itemArray = fruitsArray
case 1: destination.itemArray = vegetableArray
case 2: destination.itemArray = cheeseArray
case 3: destination.itemArray = pastaArray
default: break
}
}
}
To VC:
var itemArray = [String]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FoodItemCell") as! FoodItemCell
cell.foodItemLabel.text = itemArray[indexPath.row]
return cell
}
So, the above solution works fine. But I want to implement detailArrays in addition to the existing arrays. Theese detailArrays should be 2D arrays. The goal is something like this:
let fruitsArray = [["Oranges"], ["Apples"], ["Bannas"]]
let fruitsDescriptionArray = [["Is orange, and are round"], ["Comes in different colors, and are round"],["Is yellow and are long and curved"]]
How can I pass these types of arrays? I have tried this (in the recieving VC):
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FoodItemCell") as! FoodItemCell
cell.foodItemLabel.text = itemArray[indexPath.row][indexPath.row]
return cell
}
Also, I need to pass two or more arrays for each cell that is tapped. Is that possible with this solution (switch statement)