NEW: Learn to build amazing SwiftUI apps for macOS with my new book! >>

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!

1      

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

4      

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!

1      

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

1      

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.

1      

great! roosterboy's way works!

1      

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

4      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Spend less time managing in-app purchase infrastructure so you can focus on building your app. RevenueCat gives everything you need to easily implement, manage, and analyze in-app purchases and subscriptions without managing servers or writing backend code.

Get Started

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.