首页 > 解决方案 > 将数据数组从集合视图传递到表视图

问题描述

我需要将我的 ProductNames 数组从 HomeViewController(VCA) 传递给 ProductSelectionViewController(VCB)

问题是它只会通过第一项。例如:如果我点击名为“ITEM 1”的单元格,它只会通过“Alk1”而不是“Alk1”、“Alk2”、“Alk3”

我使用了打印语句并且它正确地传递了参数,我无法理解它不会传递整个数组的原因。

注意:segue 是从情节提要中的单元格到第二个 VC

数据模型:

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

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

主视图控制器 (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]
            }
        }
    }

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
    }

我希望表格视图显示正在发送的项目数组。即,如果点击第 1 项,则表格视图应代表“Alk1、Alk2、Alk3”,总共三个单元格。

我只为“Alk1”获取一个单元格

在 numberOfRowsInSection 中,我使用了一个 print 语句来查看有多少对象被计算在内

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

返回输出:

有 1 项

有 1 项

有 1 项

标签: iosswift

解决方案


是的,

在您的 prepareForSegue 上,您创建了一个数组,[productList]但产品列表只有一项,即被选中的一项,而不是其中的名称。

然后在您的 ProductSelectionViewController 中使用products.productName.count,因为那是您想要的数组,cellForRow这样let productItem = products[0].productName[indexPath.row]做您可以获得正确的数组。

但是,如果您传递正确的 productNames 数组而不是手动创建一个数组并插入一个Parameter对象而不是它包含的 productNames,则可以进一步改进您的代码

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

推荐阅读