Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Martin Dweh
Martin Dweh
1,014 Points

UICollectionView "didSelectItemAtIndexPath"

Hello

I'm having a little trouble with UICollectionView, didSelectItemAtIndexPath.

  • I'm working on a simple app that shows all the co-workers and bosses at my job in a CollectionView. I have a collectionView set up with 2 sections; the 1st. section is called Employees & the 2nd is Bosses. That works fine, the cells displays the names and photos of all the individuals in the correct sections.

my problem here is -Well what I want to know is; When I tap on a cell, it should take me to a new ViewController -That displays the employee's or boss's name, title, and job description.

I have the 2nd (details) ViewController set up and the employees & boss info set up in an array. But I do not know how to make this happen in "UICollectionView, didSelectItemAtIndexPath"

in short; When I tap on a cell, I want it to take me to a new view that shows me infos about that particular individual

Anything help to achieve this, will be greatly appreciated - JUST FOR TAKING YOUR TIME TO READ THIS; THANK YOU VERY MUCH

6 Answers

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Martin Dweh,

Have you tried creating a push segue from one view to another? In Main.storyboard, you need to CTRL click on the element that you are trying to transition from and drag into the view that you want to go to. Then select the type of segue you want. I also recommend creating a navigation controller, since your going from one view into another more detailed view. This is a pretty bad explanation, I'm still learning myself. I don't know if it will solve your problem (:

Good Luck!

Edit: Maybe this video you will find of some use. Using Navigation Controllers

Martin Dweh
Martin Dweh
1,014 Points

Thanks Steven

But I already have all that set up. When I tapped on a cell it takes me to the new view, BUT it doesn't display the individual's infos in the new view. - I just want to know how to pull "Photos and Text" from my "detailsArray" & "photosIDArray" and DISPLAY them in the newDetailsView that appears when I tapped on a cell

**But, THANK YOU VERY MUCH Steven

Steven Deutsch
Steven Deutsch
21,046 Points

Martin,

Hopefully, there is someone else who can help you with this in the short term. Give me a few hours and I will have your answer if no one else comes to the rescue.

Good Luck

Martin Dweh
Martin Dweh
1,014 Points

Thank you - I appreciate your help

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Martin,

This is not a solution but rather an attempt. Maybe just thing's to consider, I haven't tested this myself but I WILL figure it out. Please tell me IF or WHAT elements of this were useful to you and I'll try to troubleshoot further.

/* Step 1: In the bottom of your MasterDetailViewController you need to call 
the prepareForSegue method. */

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

 }
/* Step 2: Click on your push segue in the storyboard, and go to the attributes, 
and give it a name for the Identifier. */

// use that same identifier name in an If statement in the prepareForSegue method
 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "putIdentifierNameHere" {

        }
 }
// Step 3: In your if statement, create a constant to refer to your DetailViewController
 if segue.identifier == "putIdentifierNameHere" {
            let aNameForYourDetailView = segue.destinationViewController 
            as yourDetailViewControllerClassName
        }

/* Step 4: Create an empty array variable in your detail view under your @IBOutlets 
to store the data you're going to pass in */

var letEmptyArray = []

/*Step 5: In your MasterDetailViewController, use the constant you set for your 
DetailViewController, aNameForYourDetailView, and call the method on it that 
you want to use, I think  didSelectItemAtPath, and then pass in the value you want. */

if segue.identifier == "putIdentifierNameHere" {
    let aNameForYourDetailView = segue.destinationViewController as yourDetailViewControllerName
    aNameForYourDetailView.letEmptyArray = dataModelArray.didSelectItemAtPath(  _  )
    }
/* Step 6: Back in your Detail View, under viewDidLoad, 
you need to set the value of the element you're trying to manipulate, 
to the value of letEmptyArray (because it is no longer empty and was populated
 with values from the prepareForSegue method). */
Martin Dweh
Martin Dweh
1,014 Points

Hey Steven thanks a lot brother.

I'd found something similar that works. But now the ONLY problem is that it's performing the Segue twice. I'm guessing because it's connected in Storyboard & in the code. But when I delete it from in Storyboard, the app crashes, because there's no "Identifier" to use in the code. & If I remove the code, it just doesn't work.

The code is below, please take a look. The Segue too is linked up in the Storyboard, that's where I got the Identifier "goToWorkerDetails" from.

Problem: When I tapped on a cell, it loads the "2nd View (WorkerDetailsView)" 2 times.

Question: How can I make it load just once?

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { self.performSegueWithIdentifier("goToWorkerDetails", sender: self) }

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "goToWorkerDetails"
    {
        let indexPaths = self.myFlagsCollectionView!.indexPathsForSelectedItems()!
        let indexPath = indexPaths[0] as NSIndexPath

        let myJobWorkerInfo = segue.destinationViewController as! WorkerDetailsView

       myJobWorkerInfo.workerPhotoid = allWorkersIDArray[indexPath.row]!
    }
}

-Thank you so much for your help

Martin Dweh
Martin Dweh
1,014 Points

Okay thanks Steven, I really do appreciate all your help.

To fix the double segue, I just commented out the

//    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
//        self.performSegueWithIdentifier("goToFlagDetails", sender: self)
//    }

OR just delete the; self.performSegueWithIdentifier("goToFlagDetails", sender: self)  -That's inside the didSelectItemAtIndexPath 

I just tried it and it works.

Thanks brother for your help -I love you - you're awesome!

Steven Deutsch
Steven Deutsch
21,046 Points

Martin Dweh ,

No problem, just trying to learn myself. I can tell you have more experience than me but I like taking on problems beyond my current level. If you are looking to learn Swift or work on projects with people, we have a team Slack channel where we all collaborate. If you have an e-mail I can send you an invite.

Good Luck

Martin Dweh
Martin Dweh
1,014 Points

That sounds cool - Is there a way I can send you my email without posting it here for everyone? I'd try clicking on your facebook link on your profile, but I got an error on facebook page

Steven Deutsch
Steven Deutsch
21,046 Points

Thanks for letting me know. I have to fix that. Post it really quick and I'll invite you now, then you can delete the post. Trust me, no one is viewing this right now (:

Steven Deutsch
Steven Deutsch
21,046 Points

Try to post your replies as comments and not answers to keep the forum clean and efficient.