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

Jason Brown
Jason Brown
2,412 Points

What value should I pass through a segue to access a Detail view of an item in a collection?

I am trying to use a collection to build a relatively simple Master/Detail view program and I cannot figure out how to pass the right data through my segue to show the relevant detail view.

My data is structured as a nested array with several sections and rows. The section and row references point to a dictionary with the information I would like to present on the Detail view when someone selects an item from the collection.

To display the images in the collection I used the following structure:

let image = UIImage(named: menuList [indexPath.section][indexPath.row]["Image"]!) cell.imageView.image = image

I have tried two approaches to passing the cell information through the segue; however I can't get either to work.

I don't understand the syntax to use the find function in a nested array to pass the information the same way as the Playlist app. I also cannot find the right way to pass along the index path information from the selected cell through the segue.

Any advice on how I can pass the correct reference through would be greatly appreciated!

1 Answer

Greg Kaleka
Greg Kaleka
39,021 Points

You'll be using the indexPath for the row that was selected to pass the appropriate data to your detail view. You'll need some control flow using if or switch statements (or both!). For example:

let tableSec = indexPath.section
let tableRow = indexPath.row

switch tableSec {
    case 0:
        switch tableRow {
            case 0:
                // pass data for your array[0][0]
                break
            case 1:
                // pass data for your array[0][1]
                break
            case 2:
                // pass data for your array[0][2]
                break
            case 1:
                // pass data for your array[0][0]
                break
         }
    case 1:
        switch tableRow {
            case 0:
                // pass data for your array[1][0]
                break
    // etc.
    }
}

Make sense?

Jason Brown
Jason Brown
2,412 Points

Hi Greg,

The actual data set I'm using is quite huge, so I have simplified it somewhat to illustrate the same structure.

struct TestData {

let testItem = [

// Team A (Section One)
    [

    ["Name": "John Doe",
    "Height":"6'5",
    "Weight" : "190 Lbs",
    "Team": "A",
    "Image": "One.jpg"],

    ["Name": "Jane Doe",
    "Height":"5'5",
    "Weight" : "125 Lbs",
    "Team": "A",
    "Image": "Two.jpg"],

     ],

// Team B (Section Two)  
    [

    ["Name": "Jimmy Bean",
    "Height":"5'5",
    "Weight" : "125 Lbs",
    "Team": "B",
    "Image": "Three.jpg"],

   ["Name": "Happy Gilmore",
    "Height":"5'5",
    "Weight" : "165 Lbs",
    "Team": "B",
    "Image": "Four.jpg"],
],

// Team C (Section Three)

    [
    ["Name": "Sgt. Pepper",
    "Height":"5'5",
    "Weight" : "125 Lbs",
    "Team": "C",
    "Image": "Five.jpg"],    
]
]
}
Greg Kaleka
Greg Kaleka
39,021 Points

Thanks Jason. - you may want to re-format your comment with proper markdown. Check out the Markdown Cheatsheet link below the text area while you're building your comment.

Anyway, I'll update my answer above based on your code here.