![](/img/trans.png)
[英]CollectionView didSelectItemAtIndexPath doesn't work - Swift
[英]Swift: didSelectItemAtIndexpath function not work of json
我有三个控制器。 在第一个CategoryCollectionViewController中,在下一个listTableViewController中,最后具有DescriptionCollectionViewController。 在控制器中完美地传递了json数据。 但是我不知道我应该在ListTableViewController的didSelectRowAt_indexPath
函数中编写什么代码。
第一个CategoryCollectionViewController
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let controller1 = ListTableView()
controller1.product_id = arrCategory[indexPath.item].id!
navigationController?.pushViewController(controller1, animated: true)
}
** CategoryCollectionViewController Json Web文件**
第二个ListTableViewController
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tell me please what code i have to write here for push ViewController
}
ListTableController和DescriptionCollectionViewController的json网络文件相同
唯一不同的是product_image
值必须加载到ListTableViewController的单元格中,而all_images
值必须加载到DescriptionCollectionViewController的单元格中。
在设置情节提要ID之后,您必须编写以下代码来推送视图控制器:
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let controller1 = self.storyboard?.instantiateViewController(withIdentifier:"ListTableView") as! ListTableView
navigationController?.pushViewController(controller1, animated: true)
}
我假设StoryBoard ID与您的控制器类名称相同,因此您可以在StoryBoard->身份检查器中设置StoryBoard ID。
注意:-如果要推送另一个视图控制器,则替换标识符,就像这样:
let controller1 = self.storyboard?.instantiateViewController(withIdentifier:"DescriptionCollectionViewController") as! DescriptionCollectionViewController
您的功能将是这样。
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let controller1 = NextViewControoler()
controller1.data = dataSource["products"]["data"][indexPath.row]
navigationController?.pushViewController(controller1, animated: true)
}
描述
dataSource
将是存储JSON
数据的那个变量。
dataSource["products"]
将为您提供键值字典。 dataSource["products"]["data"]
将为您提供另一个包含data
数组的键值字典。
dataSource["products"]["data"][indexPath.row]
将为您提供选定的项目字典。
请注意,您可能需要进行一些强制转换才能获取所需的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.