ios - 将数据数组从集合视图传递到表视图
问题描述
我需要将我的 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 项
解决方案
是的,
在您的 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
推荐阅读
- javascript - 我想根据不同的语言环境翻译 boostrap 验证消息。(不使用浏览器语言)
- laravel - Laravel 5.4 控制器功能无法使用来自 ionic 3 的获取请求参数
- burp - Burp:更改目标/分支的主机名
- ios - 在 setInterval 方法中使用 getCurrentPosition geoLocation 函数通过 React-Native 获取用户当前位置时出现问题
- javascript - 角模态不会随着组件变量的变化而更新
- hibernate - Hibernate - 具有聚合功能的 COALESCE 不适用于空数据库
- appium - 由于 xcodebuild 失败,无法启动 WebDriverAgent:“xcodebuild failed with code 65
- java - 如何确定异常处理程序的响应状态?
- php - header('Content-Type: image/jpeg') 使网站变成黑色
- python-3.x - 我们如何知道模型在训练用于对象分类/检测的神经网络时收敛?