Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix wrong title header when search #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
27 changes: 19 additions & 8 deletions Source/MICountryPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ open class MICountryPicker: UITableViewController {
let displayName = (locale as NSLocale).displayName(forKey: NSLocale.Key.countryCode, value: countryCode)
let countryData = CallingCodes.filter { $0["code"] == countryCode }
let country: MICountry

if countryData.count > 0, let dialCode = countryData[0]["dial_code"] {
country = MICountry(name: displayName!, code: countryCode, dialCode: dialCode)
} else {
Expand Down Expand Up @@ -106,21 +106,25 @@ open class MICountryPicker: UITableViewController {
open var didSelectCountryClosure: ((String, String) -> ())?
open var didSelectCountryWithCallingCodeClosure: ((String, String, String) -> ())?
open var showCallingCodes = false

convenience public init(completionHandler: @escaping ((String, String) -> ())) {
self.init()
self.didSelectCountryClosure = completionHandler
}

override open func viewDidLoad() {
super.viewDidLoad()

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
createSearchBar()
tableView.reloadData()

definesPresentationContext = true
}
func backButtonPressed(sender:UIButton) {


_ = navigationController?.popViewController(animated: true)
self.dismiss(animated: true, completion: nil)
}

// MARK: Methods

Expand Down Expand Up @@ -186,20 +190,27 @@ extension MICountryPicker {
country = sections[(indexPath as NSIndexPath).section].countries[(indexPath as NSIndexPath).row]

}

if showCallingCodes {
cell.textLabel?.text = country.name + " (" + country.dialCode! + ")"
} else {
cell.textLabel?.text = country.name
}

let bundle = "assets.bundle/"
cell.imageView!.image = UIImage(named: bundle + country.code.lowercased() + ".png", in: Bundle(for: MICountryPicker.self), compatibleWith: nil)
return cell
}

override open func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if !sections[section].countries.isEmpty {
if searchController.searchBar.text!.characters.count > 0 {
if let name = filteredList.first?.name {
let index = name.index(name.startIndex, offsetBy: 0)
return String(describing: name[index])
}
return ""
}
return self.collation.sectionTitles[section] as String
}
return ""
Expand All @@ -210,8 +221,8 @@ extension MICountryPicker {
}

override open func tableView(_ tableView: UITableView,
sectionForSectionIndexTitle title: String,
at index: Int)
sectionForSectionIndexTitle title: String,
at index: Int)
-> Int {
return collation.section(forSectionIndexTitle: index)
}
Expand Down