0

I have added a simple search bar in the header of a TableViewController and implemented the delegate. Everything is working.

What is awfully weird is that the cancel is only called after the second tap.

EDIT: https://www.youtube.com/watch?v=ehnA2kmGqcQ&feature=youtu.be

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            if section == 0 {
                searchBar = UISearchBar(frame: CGRectMake(0, 0, tableView.frame.size.width, 0))
                searchBar.barTintColor = kbackgroundColor
                searchBar.showsCancelButton = true
                searchBar.showsSearchResultsButton = false
                searchBar.tintColor = UIColor.whiteColor()
                searchBar.delegate = self
                return searchBar
            }
            return nil
        }


      func searchBarCancelButtonClicked(searchBar: UISearchBar) {
            searchInProgress = false
            loadObjects()
            searchBar.resignFirstResponder()
        }
ericgu
  • 2,229
  • 23
  • 25
  • "the cancel is only called after the second tap" How do you know this? Put logging in both the "if section" stuff and the "cancelButtonClicked" stuff to be sure whether they are both being called when you expect. – matt Feb 24 '15 at 16:05
  • @matt I will make a short video and put logging. I think its a bug. If I link the video, would you watch it? – ericgu Feb 24 '15 at 16:12

2 Answers2

1

The problem is that you are misusing the Cancel button. In your video, this is what you do:

  1. You select all the text in the search field.

  2. You delete all the text in the search field.

  3. The filtering has not stopped, so you tap Cancel.

  4. Nothing happens so you tap Cancel again.

But step 3 was wrong to begin with. When all the text in the search field is removed, the filtering should have stopped. You should be detecting this situation with the search bar delegate methods and responding accordingly.

As I mentioned in my other comments, UISearchController can be a huge help here. This situation - filtering a table view - is exactly the sort of thing it is intended for. It will will help manage the search bar behavior correctly.

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

I believe this the problem: The cancel button will only fire when it is it focus

UISearchBar cancel button not responding

Community
  • 1
  • 1
ericgu
  • 2,229
  • 23
  • 25
  • But this should not _be_ a problem. Why would you tap Cancel unless there is text in the search field? And if there is text in the search field, i.e. you are typing there, it _does_ have focus. It sounds like you're using the Cancel button wrong in some way. – matt Feb 24 '15 at 16:28
  • May I ask what you are _really_ trying to do? If you are trying to filter the table view, you should not be putting a search field manually into the section one header; you should be putting UISearchController's search field into the table's header. – matt Feb 24 '15 at 16:30
  • This downloadable example project shows you how to filter the same table using a search field: https://github.com/mattneub/Programming-iOS-Book-Examples/tree/master/bk2ch08p438searchableTable3 – matt Feb 24 '15 at 16:32
  • But notice that it is preceded by two examples showing how to do the same thing in different ways. – matt Feb 24 '15 at 16:32
  • @matt Check this video out. I'll look at your example now but I respectfully disagree. I think you can use a UISearchBar in a Header. The filter is not the issue.. https://www.youtube.com/watch?v=ehnA2kmGqcQ&feature=youtu.be – ericgu Feb 24 '15 at 16:49
  • Thanks for the video. You are indeed misusing the Cancel button. If you remove all the text from the search bar, there is nothing to cancel. You are no longer filtering, so everything in the table view should just reappear. You should be using the other search bar delegate methods to detect this situation. Again, this is a good reason for using UISearchController, which will help handle this for you, as my example demonstrates. – matt Feb 24 '15 at 17:40
  • Got it. I am not deleting the text manually, but running searchBarSearchButtonClicked, which clears the text automatically. I will try to implement my own Cancel Button to circumvent the default Cancel Behavior. – ericgu Feb 24 '15 at 17:42