ios - 如何将数据从集合视图单元传递到新的视图控制器(以编程方式且没有情节提要)?
问题描述
我在将数据从集合视图单元传递到新视图控制器时遇到问题。我有集合视图单元格,每个单元格都有图像和标签,我想为每个单元格创建新的视图控制器。
我已经尝试过 UINavigationController 但现在和 navigationController 代码在 didSelectItemAt indexPath 函数下不起作用。每个人都用故事板做这个,但我没有使用它,我找不到解决方案。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("#\(indexPath.item)!")
//let cell = collectionView.cellForItem(at: indexPath) as! itemsCell
//let cell = itemler[indexPath.row]
let newView = UINavigationController(rootViewController: orderBasketController())
self.present(newView, animated: true, completion: nil)
}
代码有一个错误,即Value of type 'feedCell' has no member 'present'。
解决方案
尝试在init
方法中注入依赖项以查看控制器。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let yourCell = collectionView.cellForItem(at: indexPath) as? YourCellClass else {
return
}
let yourModel = Model(title: yourCell.title, image: yourCell.image)
let targetViewController = TargetViewController(model: yourModel)
navigationController?.pushViewController(targetViewController, animated: true)
// or if it's not in navigation controller
// present(targetViewController, animated: true, completion: nil)
}
推荐阅读
- flutter - flutter_smart_cropper 使用-sdk:minSdkVersion 16 不能小于库中声明的版本 19
- javascript - 如果孩子包含活动课程,则打开父母下拉菜单?
- sql - 使用 Azure 的可能 INSERT 语句优化查询执行时间
- osgi - 已解决 - Eclipse 4 产品从 Oxygen 迁移到 Eclipse 2020-06:ClassNotFoundException
- sorting - 如何获取具有相同名称的元素并根据 XSLT 中的子节点值对它们进行排序
- c# - 如何修复应用程序中的分辨率?
- angular - 无法在 NgFor 循环中获取 API 数据
- python - 根据论文中的说明对编码器-解码器进行建模
- python-3.x - 将 .xlsx 文件保存到 ec2,得到一个空文件作为响应
- r - 将某列的字符修剪到一定长度