13

I have a UISearchDisplayController that is in the headerview for my UITableView. I want to know when the UISearchDisplayController's searchResultsTableView is shown so I can do some other operation:

if(self.tableView == self.searchDisplayController.searchResultsTableView)

returns true all the time even when the searchResultsTableView is shown. How can I figure this out?

Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556

2 Answers2

36

This should do the trick.

[self.searchDisplayController isActive]
Mugunth
  • 14,461
  • 15
  • 66
  • 94
  • 1
    `[self.searchDisplayController isActive]` doesn't tell you whether `searchResultsTableView` is shown. It tells you that search has been activated (eg. search bar is first responder), but if you delete any existing text in search bar (but continue to remain in search mode), the `searchResultsTableView` becomes hidden, but `isActive` continues to return YES. – junjie Jan 15 '14 at 04:35
  • @junjie Are you suggesting `searchResultsTableView.hidden == NO` would be more correct? – devios1 Aug 23 '14 at 00:20
  • 2
    @chaiguy I check for `searchDisplayController.searchBar.text.length` in addition to `searchDisplayController.isActive`. Why not `searchDisplayController.searchResultsTableView.hidden == NO`? I can't remember if it worked, but the other reason is that the accessor `searchResultsTableView` initializes the table view unnecessarily even when the user is not in search mode. – junjie Aug 23 '14 at 03:38
3

How about using the delegate methods for UISearchDisplayController?

searchDisplayController:willShowSearchResultsTableView:
searchDisplayController:didShowSearchResultsTableView:
searchDisplayController:willHideSearchResultsTableView:
searchDisplayController:didHideSearchResultsTableView:

Any reason these won't work for you?

glorifiedHacker
  • 6,410
  • 2
  • 22
  • 26