Swift:didSelectItemAtIndexpath函数不工作的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网页文件** 在这里输入图像说明

第二个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的单元格中加载。

在这里输入图像说明

您必须在设置Storyboard 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。

注: – 在推另一个视图控制器的情况下replace标识符就像这样:

 let controller1 = self.storyboard?.instantiateViewController(withIdentifier:"DescriptionCollectionViewController") as! DescriptionCollectionViewController 

你的function会是这样的。

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let controller1 = NextViewControoler() controller1.data = dataSource["products"]["data"][indexPath.row] navigationController?.pushViewController(controller1, animated: true) } 

描述

  1. dataSource将是那个存储JSON数据的variables。

  2. dataSource["products"]会给你键值字典。 dataSource["products"]["data"]将为您提供另一个包含data数组的键值字典。

  3. dataSource["products"]["data"][indexPath.row]会给你select的项目字典。

请注意,您可能需要执行一些操作才能获取所需的数据。