Olli D.
Olli D.

Reputation: 59

Swift 2.0 - UISearchBar cancel button not deleting searchBar content

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

Answers (1)

mostworld77
mostworld77

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

Related Questions