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

How can I make the index of two arrays matches up? (Not dictionary)

Hi, I retrieve two sets of objects from Parse---thumbnails and fullsize images---how do I make these two have the same index, so that when I click on the thumbnail, I can use its index to view the corresponding fullsize image?

For various reasons, I don't believe dictionary works here (I've tried it). The thing with dictionary is, it can give you a value for a key, but I don't have keys and values---I have two sets of photos.

Now, this to me seems like a very common bit of code. I can't find anything online that illustrates how to properly implement a thumbnail to full-size feature (I'm using a collectionView, and a Parse backend). If anyone has any good ideas, or can explain how to accomplish this, it would be greatly appreciated.

Again: two arrays of images, click on one and it brings user to its corresponding other.

6 Answers

Why don't you just use the UICollectionViewDelegate method didSelectItemAtIndexPath indexPath: NSIndexPath) ?

Hi Gary, I do use that. Here's the problem. I have an array of thumbnails. And I have an array of images. Here's what happens. I click on a thumbnail, but it brings me to a non-corresponding image. This happens because arrays don't keep order. The arrays are made with a particular order, but that order is not kept. Using didSelectItemAtIndexPath, I get the thumbnail at indexPath, but when I say, "use this indexPath and give me the fullsize image at that indexPath," it gives me a random image. You see?

I understand the problem, I just don't know how you set up your model to store the images. One suggestion would be to sort the two arrays then pass the indexPath that is selected to the detail view controller (use prepareToSegue method). Then use the indexPath to load your UI on the detail.

I thought about using a sort method. Is that reliable? How does that work? I don't know how sorting one would match up with the sorted other.

It's crazy that no one on TreeHouse knows how to do this.

It's not crazy, it's just sounds like bad design. If you are getting two separate Arrays that should be associated but are not, there is something wrong. How are they associated in the parse database?

Okay, please help me do it better! The two arrays are separate attributes in the same Parse class (database). Here's my goal: click on a thumbnail in a collectionView and it brings user to a new view with the full-size image. I am new to programming. I have no idea how this is supposed to be done. How should this be done? Thanks.

I have no idea how your data is structured, but one option would be to have a single Image class that contains the path to the thumbnail and hires version. With parse cloud code, it is even possible to only upload the hires image, while the corresponding thumbnail is created for you. Look at the Image struct below, which would resemble the Image class in your parse database.

The following is an example how it could be done. Unfortunately, this is untested, so it may contain errors when pasting it to your project. So please take this as a general idea rather than "the thing".

struct Image {
    let title: String
    let thumbnail: NSURL
    let hires: NSURL

    init?(parseObject: PFObject) {
        guard let title = parseObject["title"] as? String else { return nil }
        guard let thumbnailPath = parseObject["thumbnail"] as? String else { return nil }
        guard let hiresPath = parseObject["hires"] as? String else { return nil }

        guard let thumbnail = NSURL(string: thumbnailPath) else { return nil }
        guard let hires = NSURL(string: hiresPath) else { return nil }

        self.title = title
        self.thumbnail = thumbnail
        self.hires = hires
    }
}

func fetchImages(completion: (images: [Image]) -> ()) {
    let query = PFQuery(className:"Image")
    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
        if error == nil {
            let images = objects.flatMap { Image(parseObject: $0) }
            completion(images)
        } else {
            print(error)
        }
    }
}

fetchImages() {
    for image in $0 {
        print("\(image.title)")
    }
}

Now that you have a single array of Image structs where each object contains thumb and hires version, you can easily access both with the same index.

Other than that, you might want to have a look at Parse relations.

Please also note that Parse will be shutting down beginning next year, so don't submit any Apps connecting to the hosted version of Parse to the App Store!

Hope that helps :)