问题
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
dataSource
will be that variable who store theJSON
data.dataSource["products"]
will give you the key-value dictionary.dataSource["products"]["data"]
will give you another key-value dictionary which contains the array ofdata
.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