1

I have set up a ViewController with UICollectionViewCells, inside of a navigation controller. I want to be able to click on the cells and then have the user be taken to a new controller depending on which cell is selected (different controller for each cell). I want the navigation bar to still appear in the new controller, and have a back button that will take the user back to the original ViewController. I have the following code inside the initial view controller to set up the collection view cells:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 2
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: playlistCellId, for: indexPath) as! playlistCoverCell

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: 100, height: 100)
}

I also register the cells correctly in viewDidLoad. What function do I use to perform an action when selecting a cell?

2 Answers2

1

you have to use:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        if indexPath.row == 0 {
            let viewController = UIViewController() // or your custom view controller
            self.navigationController?.pushViewController(viewController, animated: true)
        }
        else if indexPath.row == 1 {
         // and so on....
        }
}

Tells the delegate that the item at the specified index path was selected. The collection view calls this method when the user successfully selects an item in the collection view. It does not call this method when you programmatically set the selection.

Mat
  • 6,236
  • 9
  • 42
  • 55
  • what does indexPath.row represent? For example if I have two cells and I call this function and write this code: `if indexPath.row == 0` - Does this mean that the first cell was selected? – Marshall Scudder May 17 '17 at 23:32
  • 1
    the first cell is at index 0. If you have 3 cells you will have index 0,1,2. indexPath.section represents the section (0 if you have just one) – Mat May 17 '17 at 23:35
  • That did not work for me. This is the code for my class: `class ProfileViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout` Is it OK that it is of type UIViewController, or does it have to be a UICollectionViewController? If this is not the issue, do you have any other ideas? – Marshall Scudder May 17 '17 at 23:45
  • Never mind, I got it working. I used autocomplete to complete the function, and it left out `_ collectionView:` after the function declaration. Thanks! – Marshall Scudder May 17 '17 at 23:54
1

you can try UICollectionViewDelegate in the function enter image description here

you can use indexPath to get elements of the current click; push to next viewController you have to have navigationViewController, if navigationController is nil, you can try protocol or block. Sorry, my English is not good, maybe grammar is wrong.