2016-03-03 130 views
0

我正在使用集合視圖來顯示自定義集合視圖單元格的網格來表示類別圖標,這些圖標將作爲按鈕繼續到表視圖控制器以顯示類別列表。我使用的「產品」(標籤)和「圖像」(產品的圖像)顯示多個定製單元陣列基於關閉的原型本身Segueing自定義視圖單元格到自定義視圖控制器

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 



    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell 

    cell.imageView?.image = self.images[indexPath.row] 

    cell.labelView?.text = self.products[indexPath.row] 

    return cell 
} 

如何將能夠Segue公司我的主頁查看控制器到單個自定義表格視圖控制器以顯示不同的類別列表,具體取決於選擇了哪個圖標?任何幫助非常感謝,仍然習慣xcode和迅速

+0

我需要一些澄清。 你在問如何從一個VC到另一個?或者詢問如何根據選擇的細胞選擇不同的風險投資?或者如何鑑別細胞的選擇? – PeejWeej

+0

接近不同的風險投資公司,取決於選擇哪個單元格,這與我所得到的最接近。我希望每個單元格在選擇時都以相同的方式在tableview中顯示類別列表(這是HW的基本購物應用程序)。我希望能夠創建一個原型表格視圖單元格,左邊是一個imageView,一個描述是所選類別單元格中每個「列表」中心的標籤。 – rws

回答

0

如果我正確地理解你,你可以使用相同的子UITableViewController並根據選擇的單元格傳遞不同的數據。

首先在CollectionViewController實施

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 

然後與數據

performSegue("identifier", sender: self.products[indexPath.row]) 
+0

我的下一個問題是如何分配一個包含UIImage數組的對象(列表圖片)和一個字符串數組(列表說明),以便選擇特定圖標。這個對象是performSegue方法中的發送者嗎? – rws

+0

也許我可以使用一系列case語句來確定在主視圖控制器中的特定索引處點擊圖標後哪些數據顯示在tableview控制器中? – rws

+0

理想情況下,您應該將數據存儲在一個數組中,索引到您用於文本/圖像數組的相同索引,而不僅僅是將必要的數據傳遞給您的下一個ViewController – PeejWeej

0

首先您的視圖控制器具有執行SEGUE符合UICollectionViewDelegate,那麼你必須執行此委託方法執行在選擇細胞時延續:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    performSegueWithIdentifier("identifier", sender: nil) 
} 

然後檢查segue標識符是否匹配,獲得所選單元格的indexPath,構建所選產品和圖像的元組,並最終將其傳遞到目標tableViewController

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "identifier" { 
     let indexPath = collectionView.indexPathsForSelectedItems()!.first! 
     let selectedProduct = (product: products[indexPath.row], image: images[indexPath.row]) 
     let controller = segue.destinationViewController as! YourTableViewController 
     controller.product = selectedProduct 
    } 
} 
相關問題