简体   繁体   English

快速查看集合视图单元格选择

[英]Collection View Cell selection using swift

Please bear with me as I am new to swift. 请耐心等待,因为我是新手。 I am trying to create a Collection view of 10 restaurants in the city of NY. 我正在尝试创建纽约市10家餐厅的“收藏”视图。 When a user clicks on the restaurant (A cell containing an image of the restaurant), I want a new collection view controller to be popped up with a collection view of the restaurants menu. 当用户单击餐厅(包含餐厅图像的单元格)时,我希望使用餐厅菜单的集合视图弹出一个新的集合视图控制器。

I want to be able to use one collection view controller to show the different menu each restaurant has when a user clicks on restaurant (a Cell). 我希望能够使用一个集合视图控制器来显示当用户单击餐厅(一个单元)时每个餐厅具有的不同菜单。

I am using one prototype cell to populate all 10 restaurants. 我正在使用一个原型单元来填充所有10家餐厅。 My difficulty now is how to get each cell to pop up that menu collection view controller with the set of menu for the particular restaurant chosen. 我现在的困难是如何使每个单元格弹出带有所选特定餐厅菜单集的菜单集合视图控制器。 The Menu collection view controller will show an image of the food, the name and the price( Almost like the way Instacart shows groceries when you choose a store. For now, I am only able to tap any cell and the same menu appears. 菜单集合视图控制器将显示食物,名称和价格的图像(几乎就像您选择商店时Instacart显示杂货的方式一样。现在,我只能点按任何一个单元格,并显示相同的菜单。

This is my restaurant list collection view controller. 这是我的餐厅列表集合视图控制器。 The DidSelect that has been commented was me trying to get the cells to be selected based on their Index path. 我曾经评论过的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);
}
    }

Below is my RestaurantCollectionViewCell 以下是我的RestaurantCollectionViewCell

     import UIKit

    class RestaurantCollectionViewCell: UICollectionViewCell {

             @IBOutlet weak var manhattanImageView: UIImageView!
    }

My issue appears to be a little bit similar this question. 我的问题似乎与此问题有点相似。 I followed the answer and i'm still not able to get it to work. 我遵循了答案,但仍然无法正常工作。

Swift: UICollectionView selecting cell indexPath issues Swift:UICollectionView选择单元格indexPath问题

Thanks. 谢谢。

I will proceed assuming you will have 10 menu collection view controllers. 我将假设您有10个菜单集合视图控制器。 This advice would apply to having only one view controller, except instead of a segueIdentifier , there would be a menu controller data property. 该建议适用于仅具有一个视图控制器,除了菜单控制器data属性而不是segueIdentifier之外。


I would recommend creating a data model to hold the Rest information. 我建议创建一个数据模型来保存其余信息。

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

Turn your yourRest property into an array of MyRest objects. yourRest属性转换为MyRest对象的数组。

var yourRestX = [MyRest]()

Then in viewDidLoad() 然后在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"),
]

Of course, this means you will need 10 menu collection view controllers each named as listed. 当然,这意味着您将需要10个菜单集合视图控制器,每个控制器的名称均如所列。 (Not a great solution, but it all depends on your needs). (这不是一个很好的解决方案,但这完全取决于您的需求)。

In collectionView(,cellForItemAtIndexPath) collectionView(,cellForItemAtIndexPath)

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

Then in collectionView(,didSelectItemAtIndexPath) 然后在collectionView(,didSelectItemAtIndexPath)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM