2016-01-25 31 views
2

請耐心等待,因爲我對swift很陌生。 我正在嘗試創建紐約市10家餐館的收藏視圖。當用戶點擊餐廳(包含餐廳圖像的單元格)時,我想要彈出一個新的收藏視圖控制器,並顯示餐館菜單的收藏視圖。收藏查看使用swift進行單元格選擇

我希望能夠使用一個集合視圖控制器來顯示每個餐廳在用戶點擊餐廳(單元格)時所具有的不同菜單。

我使用一個原型單元來填充所有10個餐廳。我現在的困難是如何讓每個單元格彈出菜單集合視圖控制器,併爲所選的特定餐廳設置菜單。菜單收藏視圖控制器將顯示食物的圖像,名稱和價格(幾乎像Instacart在選擇商店時顯示食品雜貨的方式。現在,我只能點擊任何單元格並出現相同的菜單。

這是我的餐廳列表集合視圖控制器。已經評價是我試圖讓基於其指數路徑上選擇的細胞DidSelect。

import UIKit 

    private let reuseIdentifier = "ManhattanCell" 

    class ManhattanCollectionViewController: UICollectionViewController { 

var yourRest = [String]() 


override func viewDidLoad() { 
    super.viewDidLoad() 
    yourRest = ["RestAwash", 
     "RestZoma", 
     "RestLeBaobab", 
     "RestSokhna", 
     "RestCoumba", 
     "RestMassawa", 
     "RestLesAmbassades", 
     "RestPonti", 
     "RestBraai", 
     "RestNewIvoire"] 
} 

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of items 
    return yourRest.count 
} 

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! RestaurantCollectionViewCell 

    // Configure the cell 

    let image = UIImage(named: yourRest[indexPath.row]) 
    cell.manhattanImageView.image = image 

    return cell 
} 

    /* override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    if indexPath.row == 0 { 
     // call your alert here 
     self.performSegueWithIdentifier("awashShowDetail", sender: self) 

    } else if indexPath.row == 1 { 
     self.performSegueWithIdentifier("testView", sender: self) 
    } 
} 
    */ 




    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 

    let sideSize = collectionView.frame.size.width/2.51; 
    return CGSize(width: sideSize, height: sideSize); 
} 
    } 

下面是我RestaurantCollectionViewCell

 import UIKit 

    class RestaurantCollectionViewCell: UICollectionViewCell { 

      @IBOutlet weak var manhattanImageView: UIImageView! 
    } 

我的問題e似乎有點類似這個問題。我遵循了答案,但仍然無法實現。

Swift: UICollectionView selecting cell indexPath issues

感謝。

+0

你是如何映射細胞對不同塞格斯? –

+0

我只有一個原型單元格,我將其關聯到MenuCollectionViewController。或者,我必須爲每個餐廳名稱創建10個不同的單元格,然後將每個單元格放到MenuCollectionViewController中以顯示每個餐廳的菜單? – dechairman

+0

這取決於你想要什麼。如果每個菜單集合都會有不同的外觀,那麼您需要爲每個菜單集合製作一個自定義視圖控制器。如果您可以想出一種方法來爲每個菜單集合即時交換數據,那麼您只需要一個視圖控制器。 –

回答

0

我將繼續假設您將有10個菜單集合視圖控制器。這個建議將適用於只有一個視圖控制器,除了代替segueIdentifier之外,將會有菜單控制器數據屬性。


我建議創建一個數據模型來保存其餘信息。

struct MyRest { 
    let imageName: String 
    let segueIdentifier: String 
} 

把你yourRest財產爲MyRest對象的數組。

var yourRestX = [MyRest]() 

然後在viewDidLoad()

yourRest = [ 
    MyRest(imageName: "RestAwash", segueIdentifier: "awashShowDetail"), 
    MyRest(imageName: "RestZoma", segueIdentifier: "zomaShowDetail"), 
    MyRest(imageName: "RestLeBaobab", segueIdentifier: "leBaobabShowDetail"), 
    MyRest(imageName: "RestSokhna", segueIdentifier: "sokhnaShowDetail"), 
    MyRest(imageName: "RestCoumba", segueIdentifier: "coumbaShowDetail"), 
    MyRest(imageName: "RestMassawa", segueIdentifier: "massawaShowDetail"), 
    MyRest(imageName: "RestLesAmbassades", segueIdentifier: "lesAmbassadesShowDetail"), 
    MyRest(imageName: "RestPonti", segueIdentifier: "rontiShowDetail"), 
    MyRest(imageName: "RestBraai", segueIdentifier: "braaiShowDetail"), 
    MyRest(imageName: "RestNewIvoire", segueIdentifier: "newIvoireShowDetail"), 
] 

當然,這意味着你需要每一個命名爲上市10菜單集合視圖控制器。 (不是一個很好的解決方案,但這一切都取決於您的需求)。

collectionView(,cellForItemAtIndexPath)

// Configure the cell 
let image = UIImage(named: yourRest[indexPath.row].imageName) 
cell.manhattanImageView.image = image 

然後在collectionView(,didSelectItemAtIndexPath)

let identifier = yourRest[indexPath.row].segueIdentifier 
self.performSegueWithIdentifier(identifier, sender: self) 
+0

謝謝你傑夫。我嘗試了你的解決方案,它工作。我現在面臨的唯一問題是,我有10個菜單集合視圖控制器和故事板只允許我從RestaurantCollectionViewCell只有一個Segue到MenuCollectionView。我創建了10個原型單元格,並且被分配給10個MenuCollectionViewController中的每一個。這工作得很好,除了我有一個錯誤,「多個原型單元格都有標識符:」ManhattanCell「。我改變了每個單元格,我只能使用一個。這是我上面代碼中的重用標識符。我在這裏做錯了嗎? – dechairman

相關問題