請耐心等待,因爲我對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
感謝。
你是如何映射細胞對不同塞格斯? –
我只有一個原型單元格,我將其關聯到MenuCollectionViewController。或者,我必須爲每個餐廳名稱創建10個不同的單元格,然後將每個單元格放到MenuCollectionViewController中以顯示每個餐廳的菜單? – dechairman
這取決於你想要什麼。如果每個菜單集合都會有不同的外觀,那麼您需要爲每個菜單集合製作一個自定義視圖控制器。如果您可以想出一種方法來爲每個菜單集合即時交換數據,那麼您只需要一個視圖控制器。 –