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

Questions about table view and textlabel.(milestone project.1-3)

Forums > 100 Days of Swift

@onqun  

HEllo everyone =)

1) I added the flags to project, I used hasSuffix("png") method to extract, but when I display it in table view they appear the way they are, estonia2x@ 3X@ etc. Then I added them into Assets.xcassetes. I dont know how to call them. should I call them one by one or is there a easier way because previous method is not working here .

2) I managed to add individual flag, I added label for country name it displays as estonia@2x etc. How can I solve this?

3) for explanation of individual country. i am have no idea how to start. should I create a new swift file, where should I write the information and how can I call them .

import UIKit

class TableTableViewController: UITableViewController {

    var flagNames = [String]()
    //var flagImage = [UIImage]()

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "Fun with Flags"
        navigationController?.navigationBar.prefersLargeTitles = true

        let fm = FileManager.default
        let path = Bundle.main.resourcePath!
        let items = try! fm.contentsOfDirectory(atPath: path)

        for item in items {
            if item.hasSuffix(".png") {
                flagNames.append(item)
            }
        }
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return flagNames.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = flagNames[indexPath.row]
        cell.imageView?.image = UIImage(named: flagNames[indexPath.row])
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let vc = storyboard?.instantiateViewController(withIdentifier: "Detail") as? DetailViewController {
            vc.selectedCountry = flagNames[indexPath.row]
            navigationController?.pushViewController(vc, animated: true)
        }
    }
}
import UIKit

class DetailViewController: UIViewController {

    @IBOutlet var imageView: UIImageView!
    @IBOutlet var countryNameLabel: UILabel!
    @IBOutlet var countryExplanation: UILabel!

    var selectedCountry: String?

    override func viewDidLoad() {
        super.viewDidLoad()

        title = selectedCountry
        navigationItem.largeTitleDisplayMode = .never
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareTapped))

        if let imageToLoad = selectedCountry {
            imageView.image  = UIImage(named: imageToLoad)
            countryNameLabel.text = selectedCountry
            countryExplanation.text = selectedCountry
        }
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.hidesBarsOnTap = true
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.hidesBarsOnTap = false
    }
    @objc func shareTapped() {
        let vc = UIActivityViewController(activityItems: [imageView.image!], applicationActivities: [])
        vc.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem
        present(vc, animated: true)
    }
}

3      

@onqun  

Okay I solved most of it, but I am still stucked at the main screen. Xcode includes all the versions of the flags. 2x,3x. I believe something wrong with my suffix method.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.