TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

challenge 1 parte three

Forums > 100 Days of Swift

Hi all!

I feel very stupid because I can’t figure out how to create a challenge in first project

change size, number pictures and more

Please help me :(

I really try, but for some reason I can’t understand such simple things

Thank you!!

   

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let detailVC = storyboard?.instantiateViewController(withIdentifier: "Detail") as? DetailViewController {
        detailVC.selectedImage = pictures[indexPath.row]
        detailVC.selectedImagePosition = indexPath.row + 1
        detailVC.totalNumberOfImages = pictures.count
        navigationController?.pushViewController(detailVC, animated: true)
    }
}

i wrote this in ViewController but Xcode gives out mistake

   

Hi @Vladislava! Your code seems to be totally correct. What kind of error do you have?

   

yes I am studying 100 days of Swift, but thank you ;)

   

'tableView(_:didSelectRowAt:)' has already been overridden

Value of type 'DetailViewController' has no member 'selectedImagePosition'

Value of type 'DetailViewController' has no member 'totalNumberOfImages'

xcode gives me these errors maybe I placed this code in the wrong place?

   

Well seems like you have two of those functions in your code. Leave the one that you posted above and just delete the one with the same name. All your logic for selection for the table should be handled by that function.

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

as for the latter two errors: You have to add those variables to your Detail View Controller and then use them to display information.

class DetailViewController: UIViewController {
  @IBOutlet var imageView: UIImageView!
  var selectedImage: String?
  var selectedImagePosition = 0 // Add this
  var totalNumberOfImages = 0 // Add this

  /// more code here...

  }

   

this code works!!! Please tell me how you found out that you need to delete the upper code and add these two lines to Detail View Controller? because I also want to understand how it all works because the projects will be more complicated in the future thank you so much!! i live in Ukraine and it is really the only forum where I can learn Swift thank you ;)

   

Every skill takes time to learn. You will learn more with practice. So be patient and keep coding.

In simple words this part of code override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) belongs to UITableView class and was written by developers, that is why there is a word override before func, meaning you are overriding the code of the class which contains default values. You cannot override it twice in the same part of code that is why XCode emits error.

As for the second question. You are passing the values to another class. But another class has to accept those values in the first place. So you create vars in the second class and then when you initiate that class you are just passing it. Think like so: in order to work and exist class B requires var C and var D. So you are creating that class like we created DetailedView and passed tha values C and D, and you can do whatever you want with them. BUT if you make your class B without any vars C and D and then trying to pass them to it. It says what the heck you are giving to me, i don't need that rubbish, leave me alone :)

You may also want to buy this book. I am not associated with the author in any way, just my recommendation. I still at times refer to that book, when i need to refresh my knowledge or learn something new. I know hard copy might be a bit expensive, but you can by ebook which is only 10$, almost free taking into account information in contains...

https://www.formasterminds.com/uikit_for_masterminds/

   

thank you very much for helping me!)

   

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!

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.