Skip to content

Commit

Permalink
+ Added SectionHeader Functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
SURYAKANTSHARMA committed Dec 16, 2017
1 parent 25d7dba commit a83b09c
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CountryCodeInSwift3.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
72BF3C401E4D8F3D0002A99E /* Country.swift in Sources */ = {isa = PBXBuildFile; fileRef = 72BF3C371E4D8F3D0002A99E /* Country.swift */; };
72BF3C411E4D8F3D0002A99E /* CountryManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 72BF3C381E4D8F3D0002A99E /* CountryManager.swift */; };
72BF3C421E4D8F3D0002A99E /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 72BF3C391E4D8F3D0002A99E /* Images.xcassets */; };
E073F3181FE5842E00197587 /* CountryPickerWithSectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E073F3171FE5842E00197587 /* CountryPickerWithSectionViewController.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -52,6 +53,7 @@
72BF3C371E4D8F3D0002A99E /* Country.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Country.swift; sourceTree = "<group>"; };
72BF3C381E4D8F3D0002A99E /* CountryManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CountryManager.swift; sourceTree = "<group>"; };
72BF3C391E4D8F3D0002A99E /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
E073F3171FE5842E00197587 /* CountryPickerWithSectionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CountryPickerWithSectionViewController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -124,6 +126,7 @@
72BF3C371E4D8F3D0002A99E /* Country.swift */,
72BF3C381E4D8F3D0002A99E /* CountryManager.swift */,
72BF3C391E4D8F3D0002A99E /* Images.xcassets */,
E073F3171FE5842E00197587 /* CountryPickerWithSectionViewController.swift */,
);
path = CountryManager;
sourceTree = "<group>";
Expand Down Expand Up @@ -256,6 +259,7 @@
buildActionMask = 2147483647;
files = (
3606D6271E079D7D00EB417A /* ViewController.swift in Sources */,
E073F3181FE5842E00197587 /* CountryPickerWithSectionViewController.swift in Sources */,
3606D6251E079D7D00EB417A /* AppDelegate.swift in Sources */,
72BF3C3F1E4D8F3D0002A99E /* Constant.swift in Sources */,
72BF3C3A1E4D8F3D0002A99E /* CountryTableViewCell.swift in Sources */,
Expand Down
Binary file not shown.
24 changes: 7 additions & 17 deletions CountryCodeInSwift3/CountryManager/CountryPickerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
import UIKit

class CountryPickerController: UIViewController {

//MARK:- Variables
fileprivate var countries = [Country]()
fileprivate var filterCountries = [Country]()
fileprivate var applySearch = false
var countries = [Country]()
var filterCountries = [Country]()
var applySearch = false
fileprivate var _presentingViewController: UIViewController?
fileprivate var callBack : (( _ chosenCountry: Country) -> Void)?
var callBack : (( _ choosenCountry: Country) -> Void)?
fileprivate var searchController: UISearchController?

public var statusBarStyle : UIStatusBarStyle? = .default
Expand Down Expand Up @@ -122,7 +123,6 @@ class CountryPickerController: UIViewController {
tableView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
tableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)

])
} else {
// Fallback on earlier versions
Expand All @@ -137,7 +137,7 @@ class CountryPickerController: UIViewController {

@discardableResult
class func presentController(on viewController: UIViewController, callBack:@escaping (_ chosenCountry: Country)->Void) -> CountryPickerController{
let controller = CountryPickerController()
let controller = CountryPickerWithSectionViewController()
controller._presentingViewController = viewController
controller.callBack = callBack
let navigationController = UINavigationController(rootViewController: controller)
Expand Down Expand Up @@ -208,8 +208,7 @@ extension CountryPickerController: UITableViewDelegate, UITableViewDataSource {
cell.separatorLine.backgroundColor = self.separatorLineColor
return cell
}

return UITableViewCell()
fatalError("Cell with Identifier CountryTableViewCell cann't dequed")
}

//MARK:- TableView Delegate
Expand All @@ -232,18 +231,9 @@ extension CountryPickerController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50.0
}

}

extension CountryPickerController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
}
}



extension CountryPickerController: UISearchBarDelegate {

// MARK: - SearchBar Delegate
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
//code for filter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
//
// CountryPickerWithSectionViewController.swift
// CountryCodeInSwift3
//
// Created by SuryaKant Sharma on 16/12/17.
// Copyright © 2017 Suryakant. All rights reserved.
//

import UIKit

class CountryPickerWithSectionViewController: CountryPickerController {
//MARK:- Variables
var sections: [Character] = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
var sectionCoutries = [Character: [Country]]()

//MARK:- View Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = nil
tableView.delegate = nil

}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
fetchSectionCountries()
tableView.dataSource = self
tableView.delegate = self
}

func fetchSectionCountries() {
for section in sections {
let sectionCountries = countries.filter({ (country) -> Bool in
return country.countryName.first! == section
})
sectionCoutries[section] = sectionCountries
}
}
}

//MARK:- TableView DataSource
extension CountryPickerWithSectionViewController {
func numberOfSections(in tableView: UITableView) -> Int {
return applySearch ? 1 : sections.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return applySearch ? filterCountries.count : numberOfRowFor(section: section)
}

func numberOfRowFor(section: Int) -> Int {
let character = sections[section]
return sectionCoutries[character]!.count
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section].description
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "CountryTableViewCell") as? CountryTableViewCell {
cell.accessoryType = .none
cell.checkmarkImageView.isHidden = true

let image = UIImage(named: "tickMark")!.withRenderingMode(.alwaysTemplate)
cell.checkmarkImageView.image = image

var country: Country
if applySearch {
country = filterCountries[indexPath.row]
} else {
let character = sections[indexPath.section]
country = sectionCoutries[character]![indexPath.row]
}

if let _country = CountryManager.shared.lastCountrySelected {
let isEqual = (country.countryCode == _country.countryCode)
if isEqual{
cell.checkmarkImageView.isHidden = false
}
}
cell.nameLabel?.text = country.countryName
cell.diallingCodeLabel?.text = country.dialingCode()

let imagePath = country.imagePath
if let image = UIImage(named: imagePath, in: nil, compatibleWith: nil){
cell.flagImageView?.image = image
}

cell.nameLabel.font = self.labelFont
cell.nameLabel.textColor = self.labelColor
cell.diallingCodeLabel.font = self.detailFont
cell.diallingCodeLabel.textColor = self.detailColor
cell.flagImageView.isHidden = self.isHideFlagImage
cell.diallingCodeLabel.isHidden = self.isHideDiallingCode
cell.separatorLine.backgroundColor = self.separatorLineColor
return cell
}
fatalError("Cell with Identifier CountryTableViewCell cann't dequed")
}
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return sections.map{String($0)}
}
func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
return sections.index(of: Character(title))!
}
}

//MARK:- TableViewDelegate
extension CountryPickerWithSectionViewController {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch applySearch {
case true:
let country = filterCountries[indexPath.row]
self.callBack?(country)
CountryManager.shared.lastCountrySelected = country
case false:
let character = sections[indexPath.section]
let country = sectionCoutries[character]![indexPath.row]
self.callBack?(country)
CountryManager.shared.lastCountrySelected = country
}
tableView.deselectRow(at: indexPath, animated: true)
tableView.reloadData()
self.dismiss(animated: true, completion: nil)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,5 @@ class CountryTableViewCell: UITableViewCell {
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var diallingCodeLabel: UILabel!
@IBOutlet weak var separatorLine: UIImageView!





}
6 changes: 2 additions & 4 deletions CountryCodeInSwift3/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ class ViewController: UIViewController {

@IBAction func countryCodeButtonClicked(_ sender: UIButton) {

let countryController = CountryPickerController.presentController(on: self) { (country: Country) in
let countryController = CountryPickerWithSectionViewController.presentController(on: self) { (country: Country) in
self.countryImageView.image = country.flag
self.countryCodeButton.setTitle(country.dialingCode(), for: .normal)

}
countryController.detailColor = UIColor.red

}

}
}

0 comments on commit a83b09c

Please sign in to comment.