Reputation: 59
I have a tableView with a searchBar. The Table with the search function is working fine. I just have an unexpected behavior of the "Cancel" button of the searchBar.
The problem is that all items should be shown in tableView again after pressing "cancel" to stop filtering. This is working when I press the light grey "x" delete-button in the search text field first and then the "cancel" button to close the keypad.
If I only press "cancel" without "delete", the keypad will close, the text in the searchBar will be deleted but the table will still show the result of the search.
Is there a function to call the delete-button of the searchBar without manual pressing it?
I tried to do it this way but it is not working:
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
// called when cancel button pressed
self.searchBar.text = reset
self.view.endEditing(true)
tableView.reloadData()
}
Can someone help?
Upvotes: 0
Views: 1464
Reputation: 427
You can reset the Searchbar like this:
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.text = nil
searchBar.showsCancelButton = false
searchBar.endEditing(true)
self.tableView.reloadData()
}
Upvotes: 0