I have a UICollectionView that is populated by an array of productItem structs, the struct blueprint is in it's own swift file called ProductoItem.swift
struct ProductoItem {
let id: Int
let name: String
var price: String
var picture: String
var description: String
}
This works and I can successfully populate my UICollectionView with it, what I want to do now is to pass this struct to a detailView so I create this variable in the detailView.
var productos = [ProductoItem]()
And I put the following code in my prepareForSegue Method.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "detalleProducto" {
let controller = segue.destination as! ProductoDetailViewController
controller.productos = productos
}
}
What I don't know how to do know is:
First as we are sending the entire array of structs I want to pass the values stored on the CollectionView cell's index to the DetailView to populate it using just the data of the product's cell, then I also want to have two buttons to go to the next / prev product that just go to the next / prev index value of my struct. How can I do this?
First, create a new 'currentIndex: Int' attribute in your detailView.
From your first UIViewController (the one that has the UICollectionView), pass the current index and the array to your DetailView:
Then in your DetailView, you have your array and the current index, so you can just get the info of this specific product by doing:
To get to the previous or next one, you can just increment or decrement 'currentIndex'.