简体   繁体   English

斯威夫特:didSelectItemAtIndexpath函数不适用于json

[英]Swift: didSelectItemAtIndexpath function not work of json

i have three Controller. 我有三个控制器。 in first CategoryCollectionViewController, next listTableViewController and finally has DescriptionCollectionViewController. 在第一个CategoryCollectionViewController中,在下一个listTableViewController中,最后具有DescriptionCollectionViewController。 in Controllers passed json data perfectly. 在控制器中完美地传递了json数据。 but i got no idea what code i should write in didSelectRowAt_indexPath function of ListTableViewController. 但是我不知道我应该在ListTableViewController的didSelectRowAt_indexPath函数中编写什么代码。

The first CategoryCollectionViewController 第一个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 file** ** CategoryCollectionViewController Json Web文件** 在此处输入图片说明

The second ListTableViewController 第二个ListTableViewController

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

tell me please what code i have to write here for push ViewController

}

ListTableController and DescriptionCollectionViewController's json web file is same ListTableController和DescriptionCollectionViewController的json网络文件相同

just different is product_image value have to load in ListTableViewController's cell and all_images value have to load in DescriptionCollectionViewController's cell. 唯一不同的是product_image值必须加载到ListTableViewController的单元格中,而all_images值必须加载到DescriptionCollectionViewController的单元格中。

在此处输入图片说明

You have to write this code for pushing view-controller after setting the Storyboard Id : 在设置情节提要ID之后,您必须编写以下代码来推送视图控制器:

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let controller1 = self.storyboard?.instantiateViewController(withIdentifier:"ListTableView") as! ListTableView
    navigationController?.pushViewController(controller1, animated: true)
}

I am assuming that the StoryBoard Id will be same as your controller class name so you can set StoryBoard Id in StoryBoard -> identity inspector. 我假设StoryBoard ID与您的控制器类名称相同,因此您可以在StoryBoard->身份检查器中设置StoryBoard ID。

NOTE:- Replace the identifier in case of push another view controller Just like that: 注意:-如果要推送另一个视图控制器,则替换标识符,就像这样:

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

You function will be something like that. 您的功能将是这样。

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

Description 描述

  1. dataSource will be that variable who store the JSON data. dataSource将是存储JSON数据的那个变量。

  2. dataSource["products"] will give you the key-value dictionary. dataSource["products"]将为您提供键值字典。 dataSource["products"]["data"] will give you another key-value dictionary which contains the array of data . dataSource["products"]["data"]将为您提供另一个包含data数组的键值字典。

  3. dataSource["products"]["data"][indexPath.row] will give you the selected item dictionary. dataSource["products"]["data"][indexPath.row]将为您提供选定的项目字典。

Please note that you might need to do some casting to get your required data. 请注意,您可能需要进行一些强制转换才能获取所需的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM