Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
20 views3 pages

Message

This document contains code for a CountriesTableViewController class that displays a table view with country data grouped by continent. It implements table view data source methods to populate the sections and rows from country data and select rows. It also includes code for navigation and segues to a detail view controller.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Message

This document contains code for a CountriesTableViewController class that displays a table view with country data grouped by continent. It implements table view data source methods to populate the sections and rows from country data and select rows. It also includes code for navigation and segues to a detail view controller.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import UIKit

class CoutriesTableViewController: UITableViewController {

var selectedCountry: Country?

var uniqueContinents: [String] {


return Array(Set(countries.map { $0.continent })).sorted()
}

override func viewDidLoad() {


super.viewDidLoad()

tableView.register(UITableViewCell.self, forCellReuseIdentifier:
"CountryCell")

let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self,


action: #selector(addButtonTapped))
navigationItem.rightBarButtonItem = addButton

// Uncomment the following line to preserve selection between presentations


// self.clearsSelectionOnViewWillAppear = false

// Uncomment the following line to display an Edit button in the navigation


bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {


return uniqueContinents.count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection


section: Int) -> Int {
let continent = uniqueContinents[section]
let countriesInSection = countries.filter { $0.continent ==
continent }
return countriesInSection.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath:


IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CountryCell",
for: indexPath)

let continent = uniqueContinents[indexPath.section]


let countriesInSection = countries.filter { $0.continent ==
continent }
let country = countriesInSection[indexPath.row]

cell.textLabel?.text = country.name
cell.imageView?.image = UIImage(named: country.isoCode)
return cell
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection


section: Int) -> String? {
return uniqueContinents[section]
}

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


IndexPath) {
let continent = uniqueContinents[indexPath.section]
let countriesInSection = countries.filter { $0.continent ==
continent }
selectedCountry = countriesInSection[indexPath.row]

performSegue(withIdentifier: "DetailSegue", sender: nil)


}

// Navigation

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {


if segue.identifier == "DetailSegue" {
if let detailVC = segue.destination as? DetailTableViewController {
detailVC.country = selectedCountry
}
}
}

@objc func addButtonTapped(){


performSegue(withIdentifier: "AddSegue", sender: nil)
}

/*
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath:
IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/

/*
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle:
UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the
array, and add a new row to the table view
}
}
*/

/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath:
IndexPath, to: IndexPath) {

}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath:
IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little


preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/

You might also like