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 Build a Selfie App with Swift 2 Saving and Displaying Selfies Displaying Photos

Detail View Controller

Does anyone know how to implement a detail view controller when the user taps on a photo programmatically? I'm trying to expand on the Selfie App for Swift 2 that was done with Pasan.

I know the method would take place somewhere in the PhotoDataSource.swift file after this bit of code:

extension PhotoDataSource: UICollectionViewDataSource { func numberOfSections(in collectionView: UICollectionView) -> Int { return fetchedResultsController.sections?.count ?? 0 }

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    guard let section = fetchedResultsController.sections?[section] else{return 0}

    return section.numberOfObjects
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PhotoCell.reuseIdentifier, for: indexPath) as! PhotoCell
    let photo = fetchedResultsController.object(at: indexPath) as! Photo

    cell.imageView.image = photo.photoImage

    return cell
}

// MARK:- prepareForSegue

A point in the right direction would be greatly appreciated.

1 Answer

I'm guessing you should override the delegate method "didSelectItemAtIndexPath". What I would do is assign the detail view controller a storyboard ID, then, assuming your master view controller is a collection view controller:

class YourCollectionViewController: UICollectionViewController {

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let detailViewController = storyboard.instantiateViewController(withIdentifier: "Your Identifier")
        self.showDetailViewController(detailViewController, sender: self)
    }

}

Of course, this isn't purely programmatic because you're using the storyboard to instantiate the view controller. I haven't taken the selfie course yet, and I've never worked with Master-Detail controllers or collection views, but I imagine that this will get you off to a good start.

Also, it just occurred to me that maybe you have a segue and a detail view controller already configured in your storyboard. In that case, all you'd have to do would be to implement performSegue(withIdentifier: "Your Segue Identifier") in the above function, instead of instantiating a new controller.