ios - 将数据数组从 Collection View 传递到 TableView

标签 ios swift

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

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

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

注意:segue 是从 Storyboard中的单元格到第二个 VC

数据模型:

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

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

主视图 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]
            }
        }
    }

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
    }

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

我只得到一个“Alk1”单元格

在 numberOfRowsInSection 中,我使用打印语句来查看有多少对象被计数

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

返回输出:

有1个项目

有1个项目

有1个项目

最佳答案

是的,

在您的 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

关于ios - 将数据数组从 Collection View 传递到 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57261444/

相关文章:

ios - 切片图像在 Storyboard中重复,但是当我运行应用程序时

ios - 在 Swift 中,如何检测 "emoji tag"?

swift - 如果它比一条语句更复杂,则变异闭包不会编译——如何修复?

ios - 生成歌曲建议(自动完成)

ios - SpriteKit SKCameraNode - 在分配后展开可选值时为零

swift - 'RLMException',原因 : 'Opening Realm files of format version 11 is not supported by this version of Realm'

swift 4.0 包 generate-xcodeproj 不工作

ios - 后退按钮后隐藏的导航栏

iOS:如何拍摄圆角的 UIView 快照?

ios - UILocalNotification 最小操作——一蓝一清?