-
-
Save zontan/4b5ee9704b37f9c733bb06168bf4448c to your computer and use it in GitHub Desktop.
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
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { | |
resultsArray.removeAll() | |
tableView.reloadData() | |
} | |
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { | |
if let searchText = searchBar.text?.lowercased(), searchText != "" { | |
resultsArray.removeAll() | |
queryText(searchText, inField: "username") | |
} else { | |
let alert = UIAlertController(title: "Error", message: "Please enter a username.", preferredStyle: .alert) | |
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil)) | |
present(alert, animated: true, completion: nil) | |
} | |
} | |
func queryText(_ text: String, inField child: String) { | |
userRef.queryOrdered(byChild: child) | |
.queryStarting(atValue: text) | |
.queryEnding(atValue: text+"\u{f8ff}") | |
.observeSingleEvent(of: .value) { [weak self] (snapshot) in | |
for case let item as DataSnapshot in snapshot.children { | |
//Don't show the current user in search results | |
if self?.currentUser?.uid == item.key { | |
continue | |
} | |
if var itemData = item.value as? [String:String] { | |
itemData["uid"] = item.key | |
self?.resultsArray.append(itemData) | |
} | |
} | |
self?.tableView.reloadData() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment