LAST CHANCE: Save 50% on all my Swift books and bundles! >>

SOLVED: Project 1 - Challange 3 - Clarification needed.

Forums > 100 Days of Swift

Hi all,

Challange:

Rather than show image names in the detail title bar, show “Picture X of Y”, where Y is the total number of images and X is the selected picture’s position in the array. Make sure you count from 1 rather than 0.

With some help of our forum members I've completed. My code works, but there are two ways that I can make it work. But I'm not 100% sure how it works, specifically my 'selectedPictureNumber' variable.

Relevant main VC code:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let vc = storyboard?.instantiateViewController(withIdentifier: "Detail") as? DetailViewController {
            vc.selectedImage = pictures[indexPath.row]
            vc.totalPictures = indexPath.count
            vc.selectedPictureNumber = indexPath.row + 1

//            vc.title = "Picture \(indexPath.row + 1) of \(pictures.count)"

            navigationController?.pushViewController(vc, animated: true)
        }
    }

Relevant Detail VC code:

  var selectedImage: String?
    var selectedPictureNumber = 0
    var totalPictures = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "Picture \(selectedPictureNumber) of \(totalPictures)"

My question is, as you can see vc.totalPictures = indexPath.count but where does 'indexPath' actually reference to in my code? I never declared this variable anywhere. How does that work?

Also

vc.totalPictures = pictures.count 

Also works perfectly fine, but doesn't work for the 'totalPictures' variable since vc.selectedPictureNumber = pictures[indexPath.row + 1] will throw an error.

I'm sure the code that I've given is the correct way of doing it, but i'm not sure how this code works exactly:

vc.selectedPictureNumber = indexPath.row + 1

Thanks a ton!

3      

indexPath is passed into your function by the system when the user selects a row in your table view. You can see it here as part of the function declaration:

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

indexPath.row contains the row number (0-based) that the user selected.

Also, while vc.totalPictures = indexPath.count may compile and not give you any run-time errors, it's not what you want to do here. indexPath.count gives you the number of items in the indexPath, not in the pictures array. So you will end up with oddities like "Picture 4 of 2" and so on. You should use vc.totalPictures = pictures.count instead.

4      

Ok, so if you think of a tableView like an array, the purpose of the tableView(didSelectRowAt: ) method is to tell Swift what row was tapped bearing in mind that array elements are counted from zero.
The indexPath is a parameter that comes free with the tableView and the tableView is like an array that holds the data you are displaying to the user.

When the user taps on an element of the tableView, the detail view will present certain information. In order to know the total number of photos displayed in the tableView the tableView array size is required. So, vc.totalPictures = indexPath.count because the count function will return an integer equal to the number of elements in the array.

The purpose of selectedPictureNumber is to present to the user the index number of the selected photo out of the total number of photos. As people normally count from 1, but machines count array elements from zero, adding one to the indexPath.row will account for the discrepency.

If you write:

vc.selectedPictureNumber = pictures[indexPath.row + 1]

an error is sure to be thrown, because the computer will read this as:

"Go, to the pictures array and get me the element at index: indexPath.row + 1. Now assign that value to vc.selectedPictureNumber."

Since vc.selectedPictureNumber is defined as an Integer, the value of pictures[indexPath.row + 1] had better be an integer also otherwise, there will be trouble. In this case, pictures is an array of strings, meaning the code is attempting to assign a string value to a variable expecting an integer. Swift will complain about that and you will also be much happier assigning an integer.

I hope that helps.

5      

Thanks a ton everyone, I'm currently on project 2, will refer back to this when I'm back.

3      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

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.