簡體   English   中英

斯威夫特:didSelectItemAtIndexpath函數不適用於json

[英]Swift: didSelectItemAtIndexpath function not work of 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 Web文件** 在此處輸入圖片說明

第二個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的單元格中。

在此處輸入圖片說明

在設置情節提要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。

注意:-如果要推送另一個視圖控制器,則替換標識符,就像這樣:

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

您的功能將是這樣。

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數據的那個變量。

  2. dataSource["products"]將為您提供鍵值字典。 dataSource["products"]["data"]將為您提供另一個包含data數組的鍵值字典。

  3. dataSource["products"]["data"][indexPath.row]將為您提供選定的項目字典。

請注意,您可能需要進行一些強制轉換才能獲取所需的數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM