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

indexing dictionary in prepareForSegue

Right now, only the first list is showing, how do I index it that each cell will show the next list in the array from the dictionary. Thank you in advance

destination controller code

var list: List? @IBOutlet weak var scientificPhoto: UIImageView! @IBOutlet weak var secondPhoto: UIImageView! @IBOutlet weak var scientificText: UITextView! @IBOutlet weak var scientificTitle: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    if list != nil {

        scientificTitle.text = list!.title
        scientificPhoto.image = list!.firstImage
        secondPhoto.image = list!.secondImage
        scientificText.text = list!.description

    }

Table View controller code

import UIKit

class ScientificTableViewController: UITableViewController {

var miracles = ScientificLibrary()
var names = [String]()
var photos = [UIImage]()


override func viewDidLoad() {
    super.viewDidLoad()

    names = ["Universe", "Seas", "Clouds", "Tectonics", "Bees", "Rome", "Arithmetics"]      
    photos = [UIImage(named: "galaxy")!, UIImage(named: "seas")!, UIImage(named: "cloud")!, UIImage(named: "view")!, UIImage(named: "bees")!, UIImage(named: "rome")!, UIImage(named: "arithmetics")!]   
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return names.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ScientificTableViewCell

    cell.photo.image = photos[indexPath.row]
    cell.Title.text = names[indexPath.row]
    return cell
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showMiracle" {
        let scientificDetailTableViewController = segue.destinationViewController as! ScientificDetailTableViewController

        scientificDetailTableViewController.list = List(index: 0)

    }

}

}

1 Answer

Hey Mohamed Ibrahim,

I believe you are performing this segue from a UITableViewCell to another ViewController? If so, you can get the indexPath of the selected cell from tableView using

tableView.indexPathForSelectedRow()
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showMiracle" {
        let scientificDetailTableViewController = segue.destinationViewController as! ScientificDetailTableViewController
        let indexPath = tableView.indexPathForSelectedRow()
        scientificDetailTableViewController.list = List(index: indexPath.row)
}

Good Luck P.S. I'm not entirely sure how your code is working so this is not a cut and paste solution, but rather a step in the right direction.