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)
}
}