2

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. 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. 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.

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.

    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

     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

Thanks.

Community
  • 1
  • 1
dechairman
  • 45
  • 1
  • 7
  • How are you mapping the cells to different segues? – Jeffery Thomas Jan 30 '16 at 22:14
  • I just have one prototype cell that I segued to the MenuCollectionViewController. Or do I have to create 10 different cells for each restaurant name and then segue each to the MenuCollectionViewController to display the menu for each restaurant? – dechairman Jan 30 '16 at 22:18
  • That depends on what you want. If each menu collection is going to have a different look, then you will need to make a custom view controller for each one. If you can come up with a way to swap the data on the fly for each menu collection, then you will only need one view controller. – Jeffery Thomas Jan 30 '16 at 22:35

1 Answers1

0

I will proceed assuming you will have 10 menu collection view controllers. This advice would apply to having only one view controller, except instead of a segueIdentifier, there would be a menu controller data property.


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.

var yourRestX = [MyRest]()

Then in 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. (Not a great solution, but it all depends on your needs).

In collectionView(,cellForItemAtIndexPath)

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

Then in collectionView(,didSelectItemAtIndexPath)

let identifier = yourRest[indexPath.row].segueIdentifier
self.performSegueWithIdentifier(identifier, sender: self)
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • Thank you Jef. I tried your solution and it works. The only problem I am facing right now is, I have 10 menu collection view controllers and storyboard only allows me to have only one segue from the RestaurantCollectionViewCell to a MenuCollectionView. I created 10 prototype cells and segued to each of the 10 MenuCollectionViewControllers. That worked fine, except that I have an error that "Multiple prototype cells all have the identifier: "ManhattanCell". I changed for each cell and i can only use one.This is the reuseidzntifier in my code above. What do you think I am doing wrong here? – dechairman Feb 01 '16 at 02:29