Day 18 - 100 Days of Swift

2 minute read

Project 1 (part 3)

Day 18 is real simple. It is the wrap up for the the first project. He gives you a couple small challenges to work out on your own: make the title font on each cell a little bigger, display the pictures in order sorted by name, and change the title of the detail view to “Picture X of Y” where X is the number of the photo in the order and Y is the total number of photos.

First, I changed the font size of the text label in the cell. I went with 20 points and I made the font bold was well:

Screenshot of selecting font size.

Then, I added a call to .sort() on the pictures array, after it was finished being built in viewDidLoad of the viewController. This will sort the array once after it has all the pictures in it, and since we aren’t adding photos at any point, it will stay in sorted order:

1
pictures.sort()

Finally, instead of setting the title in detailViewController, I set it directly when the viewController creates a new instance of DetailViewContoller. I chose to do it here because the viewController knows how many photos there are and which one is being set on the detailViewController. Plus, it is only one line of code:

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

Another method I thought about was making two more variables in DetailViewController to hold those two numbers and passing them in didSelectRowAt. This would be more work up front, but it would mean you would have access to them if you needed them for some other reason than just the title in the detailViewController. I think you could also get a reference to the parent view controller from DetailViewController and cast is as a ViewController, and get access to the information that way. That seems like the wrong way to go about it though.

Screenshots of working app.

You can find my version of the project so far on GitHub here.

Reflections

Today was a quick and simple one. I don’t know that I have much more to say about it than that. It is nice, because I was able to get through it quickly. But at the same time that probably means I didn’t learn much today. Looking forward to the next one!