NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

SOLVED: Project 1 - challenge task 3.

Forums > 100 Days of Swift

Hi guys,

I've been trying to wrap my head around one of Paul's hints saying:

"So, we can use indexPath.row to set the selectedPictureNumber property in DetailViewController – just make sure you add 1 to it so that it counts from 1 rather than 0."

Got totally lost with how I can use indexPath.row in DetailViewController without triggering the "Unresolved Identifier" error. What am I doing wrong?

Cheers!

   

You don't use indexPath.row in DetailViewController; you use it it ViewController's didSelectRowAt to set DetailViewController.selectedPictureNumber when you instantiate that controller.

3      

Ok, thanks! For total pictures I used pictures.count - was I supposed to do it this way? Anyway, the code works, which makes my brain free for the rest of the day :D Thanks!

   

Well if it works the way you want it, it's okay. ;-)

   

Solved it also. Took some trying and figuring out, but once I had it I understood why. Also, I found that if I pulled up print(imageToLoad.count) in DetailedViewController I got an output of 12. Unsure why, possibly something to do with the Loops? So I called this .count from ViewController before passing it to DetailedViewController I hope this helps anyone, without giving away the full coded answer.

   

great! roosterboy's way works!

   

I'm late to the game, but would like to give my 2 cents too for folks that want more visual

It's something like this in ViewController.swift

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // 1: Try loading "Detail" view controller & type casting it to be DetailViewController
    if let detailVC = storyboard?.instantiateViewController(withIdentifier: "Detail") as? DetailViewController {
      // 2: If success -> set property values over in DetailViewController.swift
      // Set "selectedImage" string value
      detailVC.selectedImage = pictures[indexPath.row]
      // Set values to selectedImagePosition & totalNumberOfImages
      detailVC.selectedImagePosition = indexPath.row + 1
      // "+1" to show human-readable index number instead of Swift index number approach

      detailVC.totalNumberOfImages = pictures.count
      // Equates total count of pictures array

      // 3: Now push it onto the navigation controller
      navigationController?.pushViewController(detailVC, animated: true)
    }
  }

While over in DetailViewController.swift you declare the 2 new vars inside the overriden viewDidLoad() method

   

Hacking with Swift is sponsored by Emerge

SPONSORED Emerge helps iOS devs write better, smaller apps by profiling binary size on each pull request and surfacing insights and suggestions. Companies using Emerge have reduced the size of their apps by up to 50% in just the first day. Built by a team with years of experience reducing app size at Airbnb.

Set up a demo!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.