-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from graycampbell/search
Add search functionality
- Loading branch information
Showing
7 changed files
with
187 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// | ||
// GCSearchResultsController.swift | ||
// GCCountryPicker | ||
// | ||
// Created by Gray Campbell on 10/1/17. | ||
// | ||
|
||
import UIKit | ||
|
||
// MARK: Properties & Initializers | ||
|
||
/// The GCSearchResultsController class defines a view controller containing a search results interface. | ||
|
||
class GCSearchResultsController: UITableViewController { | ||
|
||
// MARK: Properties | ||
|
||
/// The object that acts as the delegate of the search results view controller. | ||
|
||
var delegate: GCSearchResultsDelegate? | ||
|
||
var searchResults = [GCCountry]() { | ||
|
||
didSet { | ||
|
||
self.tableView.reloadData() | ||
} | ||
} | ||
} | ||
|
||
// MARK: - View | ||
|
||
extension GCSearchResultsController { | ||
|
||
override func viewDidLoad() { | ||
|
||
super.viewDidLoad() | ||
|
||
self.configureTableView() | ||
} | ||
} | ||
|
||
// MARK: - UISearchBarDelegate | ||
|
||
extension GCSearchResultsController: UISearchBarDelegate { | ||
|
||
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { | ||
|
||
if !self.searchResults.isEmpty { | ||
|
||
let rect = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 0.5) | ||
|
||
self.tableView.scrollRectToVisible(rect, animated: true) | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Table View | ||
|
||
extension GCSearchResultsController { | ||
|
||
fileprivate func configureTableView() { | ||
|
||
self.tableView.keyboardDismissMode = .onDrag | ||
|
||
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "TableViewCell") | ||
} | ||
} | ||
|
||
// MARK: - UITableViewDelegate | ||
|
||
extension GCSearchResultsController { | ||
|
||
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | ||
|
||
return self.searchResults.count | ||
} | ||
|
||
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | ||
|
||
self.delegate?.searchResultsController(self, didSelectSearchResult: self.searchResults[indexPath.row]) | ||
} | ||
} | ||
|
||
// MARK: - UITableViewDataSource | ||
|
||
extension GCSearchResultsController { | ||
|
||
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | ||
|
||
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) | ||
|
||
cell.textLabel?.text = self.searchResults[indexPath.row].localizedDisplayName | ||
|
||
return cell | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// GCSearchResultsDelegate.swift | ||
// GCCountryPicker | ||
// | ||
// Created by Gray Campbell on 10/1/17. | ||
// | ||
|
||
import UIKit | ||
|
||
/// The delegate of a GCSearchResultsController object must adopt the GCSearchResultsDelegate protocol. | ||
|
||
protocol GCSearchResultsDelegate { | ||
|
||
/// Tells the delegate that the user picked a search result. | ||
/// | ||
/// - Parameter searchResultsController: The controller object managing the search results interface. | ||
/// - Parameter searchResult: The search result selected by the user. | ||
|
||
func searchResultsController(_ searchResultsController: GCSearchResultsController, didSelectSearchResult searchResult: GCCountry) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters