Swift: didSelectItemAtIndexpath function not work of json

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-16 07:58:31

问题


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.


回答1:


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 



回答2:


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.



来源:https://stackoverflow.com/questions/46149881/swift-didselectitematindexpath-function-not-work-of-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!