UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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!

3      

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

6      

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!

3      

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

3      

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.

3      

great! roosterboy's way works!

3      

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

6      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.