Created
July 25, 2018 06:11
-
-
Save kmuralidharan91/c3e017a1c9846f97207a2156e8db55b5 to your computer and use it in GitHub Desktop.
UITableViewController with SearchBar
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
import UIKit | |
import SwiftyJSON | |
import Alamofire | |
import SafariServices | |
final class SearchResultsTableViewController: UITableViewController { | |
private var searchResults = [JSON]() { | |
didSet { | |
tableView.reloadData() | |
} | |
} | |
private let searchController = UISearchController(searchResultsController: nil) | |
private let apiFetcher = APIRequestFetcher() | |
private var previousRun = Date() | |
private let minInterval = 0.05 | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
tableView.tableFooterView = UIView() | |
setupTableViewBackgroundView() | |
setupSearchBar() | |
} | |
private func setupTableViewBackgroundView() { | |
let backgroundViewLabel = UILabel(frame: .zero) | |
backgroundViewLabel.textColor = .darkGray | |
backgroundViewLabel.numberOfLines = 0 | |
backgroundViewLabel.text = " Oops, No results to show " | |
backgroundViewLabel.textAlignment = NSTextAlignment.center | |
backgroundViewLabel.font.withSize(20) | |
tableView.backgroundView = backgroundViewLabel | |
} | |
private func setupSearchBar() { | |
searchController.searchBar.delegate = self | |
searchController.dimsBackgroundDuringPresentation = false | |
searchController.hidesNavigationBarDuringPresentation = false | |
searchController.searchBar.placeholder = "Search any Topic" | |
definesPresentationContext = true | |
tableView.tableHeaderView = searchController.searchBar | |
} | |
override func numberOfSections(in tableView: UITableView) -> Int { | |
return 1 | |
} | |
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return searchResults.count | |
} | |
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", | |
for: indexPath) as! CustomTableViewCell | |
cell.titleLabel.text = searchResults[indexPath.row]["title"].stringValue | |
cell.descriptionLabel.text = searchResults[indexPath.row]["terms"]["description"][0].string | |
if let url = searchResults[indexPath.row]["thumbnail"]["source"].string { | |
apiFetcher.fetchImage(url: url, completionHandler: { image, _ in | |
cell.wikiImageView.image = image | |
}) | |
} | |
return cell | |
} | |
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
let title = searchResults[indexPath.row]["title"].stringValue | |
guard let url = URL.init(string: "https://en.wikipedia.org/wiki/\(title)") | |
else { return } | |
let safariVC = SFSafariViewController(url: url) | |
present(safariVC, animated: true, completion: nil) | |
tableView.deselectRow(at: indexPath, animated: true) | |
} | |
} | |
extension SearchResultsTableViewController: UISearchBarDelegate { | |
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { | |
searchResults.removeAll() | |
guard let textToSearch = searchBar.text, !textToSearch.isEmpty else { | |
return | |
} | |
if Date().timeIntervalSince(previousRun) > minInterval { | |
previousRun = Date() | |
fetchResults(for: textToSearch) | |
} | |
} | |
func fetchResults(for text: String) { | |
print("Text Searched: \(text)") | |
apiFetcher.search(searchText: text, completionHandler: { | |
[weak self] results, error in | |
if case .failure = error { | |
return | |
} | |
guard let results = results, !results.isEmpty else { | |
return | |
} | |
self?.searchResults = results | |
}) | |
} | |
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { | |
searchResults.removeAll() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment