You have a rudimentary understanding of table views, image views, and navigation controllers, so let’s put them together: your challenge is to create an app that lists various world flags in a table view. When one of them is tapped, slide in a detail view controller that contains an image view, showing the same flag full size. On the detail view controller, add an action button that lets the user share the flag picture and country name using UIActivityViewController
.
To solve this challenge you’ll need to draw on skills you learned in tutorials 1, 2, and 3:
ViewController
class so that builds on UITableViewController
instead.UIActivityViewController
to share your flag.As always, I’m going to provide some hints below, but I suggest you try to complete as much of the challenge as you can before reading them.
Hints:
let fm = FileManager.default
, then let path = Bundle.main.resourcePath!
, then finally let items = try! fm.contentsOfDirectory(atPath: path)
.hasSuffix()
method.ViewController
build on UITableViewController
, you’ll need to override its numberOfRowsInSection
and cellForRowAt
methods.tableView.dequeueReusableCell(withIdentifier: "Country", for: indexPath)
.didSelectItemAt
method is responsible for taking some action when the user taps a row.UIImageView
to load it into. The former should be modified from ViewController
inside didSelectItemAt
; the latter should be modified in the viewDidLoad()
method of your detail view controller. Bonus tip: try setting the imageView
property of the table view cell. Yes, they have one. And yes, it automatically places an image right there in the table view cell – it makes a great preview for every country.
SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.