Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 86 additions & 106 deletions CountryList/CountryList.swift
Original file line number Diff line number Diff line change
@@ -1,125 +1,105 @@
//
// CountryListTableViewController.swift
// CountryListExample
//
// Created by Juan Pablo on 9/8/17.
// Copyright © 2017 Juan Pablo Fernandez. All rights reserved.
//

import UIKit

public protocol CountryListDelegate: class {
func selectedCountry(country: Country)
}

public class CountryList: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating {
//
// CountryListTableViewController.swift
// CountryListExample
//
// Created by Juan Pablo on 9/8/17.
// Copyright © 2017 Juan Pablo Fernandez. All rights reserved.
//

var tableView: UITableView!
var searchController: UISearchController?
var resultsController = UITableViewController()
var filteredCountries = [Country]()
import UIKit

open weak var delegate: CountryListDelegate?

private var countryList: [Country] {
let countries = Countries()
let countryList = countries.countries
return countryList
public protocol CountryListDelegate: class {
func selectedCountry(country: Country)
}

// var indexList: [String] {
// var indexList: [String] = []
// for country in countryList {
// if let firstLetter = country.name?.characters.first?.description.lowercased() {
// if !indexList.contains(firstLetter) { indexList.append(firstLetter) }
// }
// }
// return indexList
// }

override public func viewDidLoad() {
super.viewDidLoad()
public class CountryList: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating {

self.title = "Country List"
self.view.backgroundColor = .white
var tableView: UITableView!
var searchController: UISearchController?
var resultsController = UITableViewController()
var filteredCountries = [Country]()

tableView = UITableView(frame: view.frame)
tableView.register(CountryCell.self, forCellReuseIdentifier: "Cell")
tableView.tableFooterView = UIView()
tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = UIColor.clear
open weak var delegate: CountryListDelegate?

self.view.addSubview(tableView)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .stop, target: self, action: #selector(handleCancel))

setUpSearchBar()
}

public func updateSearchResults(for searchController: UISearchController) {

filteredCountries.removeAll()
private var countryList: [Country] {
let countries = Countries()
let countryList = countries.countries
return countryList
}

let text = searchController.searchBar.text!.lowercased()
override public func viewDidLoad() {
super.viewDidLoad()

self.view.backgroundColor = .white

tableView = UITableView(frame: view.frame)
tableView.register(CountryCell.self, forCellReuseIdentifier: "Cell")
tableView.tableFooterView = UIView()
tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = UIColor.clear

self.view.addSubview(tableView)

setUpSearchBar()
}

for country in countryList {
guard let name = country.name else { return }
if name.lowercased().contains(text) {

filteredCountries.append(country)
public func updateSearchResults(for searchController: UISearchController) {

filteredCountries.removeAll()

let text = searchController.searchBar.text!.lowercased()

for country in countryList {
guard let name = country.name else { return }
if name.lowercased().contains(text) {

filteredCountries.append(country)
}
}

tableView.reloadData()
}

tableView.reloadData()
}

func setUpSearchBar() {
self.searchController = UISearchController(searchResultsController: nil)
self.tableView.tableHeaderView = searchController?.searchBar
self.searchController?.hidesNavigationBarDuringPresentation = false

// self.searchController?.searchBar.backgroundImage = UIImage()
self.searchController?.dimsBackgroundDuringPresentation = false
// self.searchController?.searchBar.barTintColor = UIColor.white
self.searchController?.searchBar.placeholder = "Search"
// self.searchController?.searchBar.tintColor = Constants.Colors.mainColor
// self.searchController?.searchBar.backgroundColor = UIColor.white
self.searchController?.searchResultsUpdater = self
}

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CountryCell
let country = cell.country!

self.searchController?.isActive = false
self.delegate?.selectedCountry(country: country)
func setUpSearchBar() {
self.searchController = UISearchController(searchResultsController: nil)
self.searchController?.hidesNavigationBarDuringPresentation = false
self.searchController?.searchResultsUpdater = self
self.searchController?.dimsBackgroundDuringPresentation = false
self.searchController?.searchBar.placeholder = "Search"
navigationItem.titleView = searchController?.searchBar

}

tableView.deselectRow(at: indexPath, animated: true)
self.tableView.reloadData()
self.dismiss(animated: true, completion: nil)
}

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController!.isActive && searchController!.searchBar.text != "" {
return filteredCountries.count
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CountryCell
let country = cell.country!

self.searchController?.isActive = false
self.delegate?.selectedCountry(country: country)

tableView.deselectRow(at: indexPath, animated: true)
self.tableView.reloadData()
self.dismiss(animated: true, completion: nil)
}

return countryList.count
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) as! CountryCell
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController!.isActive && searchController!.searchBar.text != "" {
return filteredCountries.count
}

return countryList.count
}

if searchController!.isActive && searchController!.searchBar.text != "" {
cell.country = filteredCountries[indexPath.row]
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) as! CountryCell

if searchController!.isActive && searchController!.searchBar.text != "" {
cell.country = filteredCountries[indexPath.row]
return cell
}

cell.country = countryList[indexPath.row]
return cell
}

cell.country = countryList[indexPath.row]
return cell
}

func handleCancel() {
self.dismiss(animated: true, completion: nil)
}
}