【发布时间】:2016-05-01 07:24:04
【问题描述】:
请多多包涵,因为我是 swift 新手。 我正在尝试创建纽约市 10 家餐厅的收藏视图。当用户单击餐厅(包含餐厅图像的单元格)时,我希望弹出一个新的集合视图控制器,其中包含餐厅菜单的集合视图。
我希望能够使用一个集合视图控制器在用户点击餐厅(一个单元格)时显示每个餐厅的不同菜单。
我正在使用一个原型单元来填充所有 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!
}
我的问题似乎有点类似于这个问题。我按照答案进行操作,但仍然无法正常工作。
Swift: UICollectionView selecting cell indexPath issues
谢谢。
【问题讨论】:
-
如何将单元格映射到不同的 segue?
-
我只有一个原型单元,我将它连接到 MenuCollectionViewController。还是我必须为每个餐厅名称创建 10 个不同的单元格,然后将每个单元格连接到 MenuCollectionViewController 以显示每个餐厅的菜单?
-
这取决于你想要什么。如果每个菜单集合都有不同的外观,那么您将需要为每个集合创建一个自定义视图控制器。如果你能想出一种方法来为每个菜单集合动态交换数据,那么你只需要一个视图控制器。
标签: ios swift uicollectionview ios9 uicollectionviewcell