Reputation: 31
I am trying to segue from a specific cell in my CollectionView in order to access different View Controllers for each cell.
I have successfully found how to segue from a cell item.
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let selectedIndexPath = interests[indexPath.item]
performSegue(withIdentifier: "FeatureVC", sender: selectedIndexPath)
}
Here is an image of my current code
However, figuring out how to segue form a specfic cell is proving more difficult. I have looked at a few answers but nothing seems tp be very straight forward.
I am new to development so I can understand if this question has a simple solution however this will be key for the main functionality of my application so i have to get this right.
Any guidance I can receive would be greatly appreciated.
Upvotes: 0
Views: 604
Reputation: 3657
It seems like you are using the wrong delegate as per your conditions also if you want to segue only one cell you can check the IndexPath.item
change didDeselectItemAt
to didSelectItemAtIndexpath
func collectionView(_ collectionView: UICollectionView, didSelectItemAtIndexpath indexPath: IndexPath) {
if indexPath.item == yourSpecificCellIndex {
//perform segue
}
}
Upvotes: 1
Reputation: 3210
As far as I understand, you want to have your specific item in the featureVC.
So in your FeatureVC class add a variable like
var interest : Interest! // this should be the type of what you have in your interests Array
In your controller where you perform the segue add a variable on the top like
var selectedInterest: Interest!
Your function to determine which item you clicked is not correct so use:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
selectedInterest = interests[indexPath.item]
performSegue(withIdentifier: "FeatureVC", sender: nil)
}
Now you just need a little bit preparation bevor you segue, so just add:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "FeatureVC" {
let featureVC = segue.destination as! FeatureVC // your controller name
featureVC.interest = selectedInterest // here you can set the variable in your feature vc
}
}
Upvotes: 0
Reputation: 4371
First use didSelectItem
method. Then write appropriate condition in that:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
if condition // your specific condition
{
performSegue(withIdentifier: "FeatureVC", sender: selectedIndexPath)
}
}
Upvotes: 0
Reputation: 4141
If you want to select from 1 cell use didSelectItemAtIndexpath
method.
In that identify your cell using the position at indexpath.row
and then call the segue accordingly
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
var cell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
// Use any cell values to identify which one should open segue
}
Upvotes: 0