Pass 2D array from one ViewController to another

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)

Accepted Reply

So, if you want to pass 2 arrays (but they do not need be 2D arrays here), but simply:

   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"]

Create 2 var in the destination VC:

itemArray

itemDescriptionArray


then just adapt the prepare like this:

    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 ; destination.itemDescriptionArray = fruitsDescriptionArray
            case 1: destination.itemArray = vegetableArray ; destination.itemDescriptionArray = vegetebaleDescriptionArray
            case 2: destination.itemArray = cheeseArray ; destination.itemDescriptionArray = cheeseDescriptionArray
            case 3: destination.itemArray = pastaArray ; destination.itemDescriptionArray = pastaDescriptionArray
   
            default: break
            }
        }
    }

In destinationVC, if you have created a foodItemDescriptionLabel in the cell:

In tableView:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "FoodItemCell") as! FoodItemCell
      
        cell.foodItemLabel.text = itemArray[indexPath.row]
        cell.foodItemDescriptionLabel.text = itemDescriptionArray[indexPath.row]

        return cell
      
    }

Replies

With what yoiu show, I do not see the interest to have 2D arrays if there is a single item in each !


What is itemArray ?


I would understand if it is [fruitsArray, vegetableArray, cheeseArray, pastaArray], but is that the case ?


Then, in table view, do you have multiple sections (one for each type of food) ? That should seem reasonable.


then, you would write

        cell.foodItemLabel.text = itemArray[indexPath.section][indexPath.row]


But there are too many thgings to guess, so, please show more so that we can understand

I´ll try to explain better:

For example: When a user taps the fruits cell, it should present a new tableViewController. This should show the name of the fruits (fruits array) and the details of the fruits (fruitsDescriptionArray).

So, the first row in the tableViewController should be:

Oranges

Is orange, and are round

The second row should be:

Apples

Comes in different colors, and are round

The third row should be:

Banans

Is yellow and are long and curved

and so on..


I need to pass these arrays from the from the first VC to the second.


itemArray is the reciever of the array from the previous VC. (There should also be a itemDescriptionArray to hold the descriptionArray from the previous VC. This will be conneced to a descriptionLabel)



cell.foodItemLabel.text = itemArray[indexPath.section][indexPath.row]

The above code is just something i have tried to make it work, with no luck.


Basically, I just want to pass over some arrays. One, with the name of the fruits, and another with a description of the fruit.

So, if you want to pass 2 arrays (but they do not need be 2D arrays here), but simply:

   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"]

Create 2 var in the destination VC:

itemArray

itemDescriptionArray


then just adapt the prepare like this:

    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 ; destination.itemDescriptionArray = fruitsDescriptionArray
            case 1: destination.itemArray = vegetableArray ; destination.itemDescriptionArray = vegetebaleDescriptionArray
            case 2: destination.itemArray = cheeseArray ; destination.itemDescriptionArray = cheeseDescriptionArray
            case 3: destination.itemArray = pastaArray ; destination.itemDescriptionArray = pastaDescriptionArray
   
            default: break
            }
        }
    }

In destinationVC, if you have created a foodItemDescriptionLabel in the cell:

In tableView:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "FoodItemCell") as! FoodItemCell
      
        cell.foodItemLabel.text = itemArray[indexPath.row]
        cell.foodItemDescriptionLabel.text = itemDescriptionArray[indexPath.row]

        return cell
      
    }

I´m sorry for bothering you with this question. I don´t know what I was thinking when I wanted 2D arrays for this.. Thanks for the help anyways.