When using a UICollectionView, you may want to pass data from one controller to another when a user clicks on the collection view cells. In order to do this, we need to find the item that has been clicked on.
With the uicollectionview and segue setup within the storyboard, you can use the prepareForSegue function to find the selected cell from the sender as shown below in the code extract.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "editSegue" { if let destinationViewController = segue.destinationViewController as? UINavigationController { if let yourEditViewController = destinationViewController.topViewController as? YourEditViewController { if let cell = sender as? UICollectionViewCell { if let indexPath = yourCollectionView.indexPathForCell(cell) { // code here to use indexPath.row and pass to viewcontroller } } } } } } |
Once you have the indexPath you are then able to get any relevant data from you collections using indexPath.row and pass that to the awaiting view controller.
As you will see above, the awaiting controller here has an embeded navigation controller, so we use this as the segue destination controller and then take the topViewController as our awaiting controller.