简体   繁体   中英

Swift: didSelectItemAtIndexpath function not work of json

i have three Controller. in first CategoryCollectionViewController, next listTableViewController and finally has DescriptionCollectionViewController. in Controllers passed json data perfectly. but i got no idea what code i should write in didSelectRowAt_indexPath function of ListTableViewController.

The first 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** 在此处输入图片说明

The second 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

just different is product_image value have to load in ListTableViewController's cell and all_images value have to load in DescriptionCollectionViewController's cell.

在此处输入图片说明

You have to write this code for pushing view-controller after setting the Storyboard 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.

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.

  2. dataSource["products"] will give you the key-value dictionary. dataSource["products"]["data"] will give you another key-value dictionary which contains the array of data .

  3. dataSource["products"]["data"][indexPath.row] will give you the selected item dictionary.

Please note that you might need to do some casting to get your required data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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