簡體   English   中英

對每個收集視圖單元格使用單個表視圖控制器

[英]use single table view controller for each collection view cell press

我有5個名為“ Resturants”,“ shops”,“ Medical”,“ parlor”,“ parks”的集合視圖單元。 因此,現在我將其設置為10個視圖控制器,並將表視圖放置在每個5個控制器中,然后調用json數據並在表視圖中顯示。 我也有一個自定義單元格,我在其中用於所有iew controlelr表視圖

就像說,如果用戶按下Restaurant ,我將重定向到一個視圖controlellr.in,我將其稱為`baseurl / category_01的網絡服務。 因此,我將從該網址獲取名稱,地址,圖像,並在表格視圖中顯示

但是我知道這是錯誤的方法。如果我有20個集合視圖單元格。那么如果我用20個表視圖創建20個視圖控制器,那就不好了。

這是我在Swift 2.0中的第一個應用程序。

誰能建議我對我的所有10個單元格的集合視圖數據使用一個表視圖的單一視圖controlellr。

但是在我的收藏夾視圖單元格數據中,有一件事是我從一個json url獲得,因為每個數據都被指定為一個類別id。

[
{
category_id : 0
category_name : Restaurant
},
{
category_id : 1
category_name : park
},
{
category_id : 2
category_name : shops
},
{
category_id : 3
category_name : medical
},
]

就像明智的10個單元格數據一樣,我將在表格視圖中顯示。因此,當我在一個表格視圖中使用一個視圖controllelr時,我需要提及,`哪個indespath.row按下並應等於我的類別ID,shoyld會顯示相關的數據。

現在我在collectionviewcontroller.swift使用的是。我就是按下的索引路徑行,而我正導航到另一個視圖控制器

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        // You can use indexPath to get "cell number x", or get the cell like:
    let cell  = collectionView.cellForItemAtIndexPath(indexPath)

    if((indexPath.row) == 0) {

        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc: ViewController2 = storyboard.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2
        self.presentViewController(vc, animated: true, completion: nil)
        print("Layout 1 clicked")

    }
    else if((indexPath.row) == 1) {
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc: ViewController3Bank = storyboard.instantiateViewControllerWithIdentifier("ViewController3Bank") as! ViewController3Bank
        self.presentViewController(vc, animated: true, completion: nil)
        print("Layout 2 clicked")
    }

那么如何使用單個viewcontrolel1r表視圖來做到這一點..請幫助我。

謝謝

將類別名稱存儲在數組中或其他位置,只要按下單元格,您就可以通過從數組中選擇某個類別來調用特定類別,如下所示

categoryArray[indexPath.row]

然后,當要轉換到另一個視圖時,請檢查已選擇了哪個類別,然后使用相關數據填充表格視圖。 您可以像這樣設置一個結構:

func determineDataSource()
{
  switch category
  {
    case "Restaurant":
      print("restaurant")
.
.
.
}

暫無
暫無

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

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