简体   繁体   中英

Passing array of data from collection view to table view

I need to pass my array of ProductNames, to ProductSelectionViewController(VCB) From HomeViewController(VCA)

The issue is that it will only pass the first item. Ex: If I tap on the cell called "ITEM 1" it passes only "Alk1" rather than "Alk1", "Alk2", "Alk3"

I used print statements and it is passing the Parameter correctly, I can't grasp the reason it will not pass the entire array.

NOTE: segue is from the cell in storyboard to the second VC

The Data Model:

class Parameter {
    var parameterName: String
    var productName: [String]

    init(parameterName: String, productName: [String] = []) {
        self.parameterName = parameterName
        self.productName = productName
    }
}

Home View Controller (VCA):

 var parameters: [Parameter] = [
        Parameter(parameterName: "Item1", productName: ["Alk1", "Alk2", "Alk3"]),
        Parameter(parameterName: "Item2", productName: ["Cal1", "Cal2", "Cal3"]),
        Parameter(parameterName: "Item3", productName: ["mag1", "mag3", "mag2"])
    ]
    // MARK: - View life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareCollectionView()
    }

    // MARK: - UICollectionViewDataSource
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return parameters.count
    }
    @objc override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ParameterCell.identifier, for: indexPath) as? ParameterCell
            else { preconditionFailure("Failed to load collection view cell") }
        cell.parameter = parameters[indexPath.row]
        cell.backgroundColor = colors[indexPath.row]
        return cell
    }

    // MARK: - UICollectionView Delegates

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        guard let productViewController = segue.destination as? ProductSelectionViewController else {
            fatalError()
        }

        if segue.identifier == HomeViewController.productSegueIdentifier {
            if let indexPaths = collectionView.indexPathsForSelectedItems {
                let indexPath = indexPaths[0]
                print("\(String(describing: indexPath))")
                let productList = parameters[indexPath.row] as Parameter
                productViewController.products = [productList]
            }
        }
    }

The ProductSelectionViewController (VCB)

class ProductSelectionViewController: UITableViewController {
    var products = [Parameter]()


    override func viewDidLoad() {
        super.viewDidLoad()
       // label.text = products?.parameterName
        print("The sent data is: \(products)")

    }


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("Amount of Product \(products.count)")
        return products.count

    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        let productItem = products[indexPath.row].productName
        cell.textLabel?.text = productItem![indexPath.row]
        //let p = products.productNames
        //ce/ll.textLabel?.text = p[indexPath.row]
        return cell
    }

I expect the table view to present an array of the items being sent. ie if Item 1 is tapped, the table view should represent "Alk1, Alk2, Alk3" for a total of three cells.

I am only getting one cell for "Alk1"

in numberOfRowsInSection, I used a print statement to see how many object are counted with

print("There are \(products.count) items")

return output:

There are 1 items

There are 1 items

There are 1 items

Yes,

On your prepareForSegue you create an array with [productList] but product list is only one item, the one that got selected, not the names it has inside.

In your ProductSelectionViewController then use products.productName.count because thats the array you want and in cellForRow do let productItem = products[0].productName[indexPath.row] so you can get the correct one.

But you can improve your code way more if you pass the correct array of productNames instead of creating an array manually and inserting the one Parameter object instead of the productNames it contains

  let productList = parameters[indexPath.row] as Parameter
  let names: [String] = productList.productName
  productViewController.products = names

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM