6

This is extremely frustrating: I'm trying to display unfiltered results in a UISearchDisplayController even if the search string is empty.

Adding debug code to numberOfSectionsInTableView: and numberOfRowsInSection: shows that when my search string is empty, non-zero integers are being returned, but cellForRowAtIndexPath: is never called! The tableview appears to be hidden.

I tried setting tableview.hidden=NO; in searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView to no avail.

This can't be that hard to pull off, I'd imagine this is a very common thing to do!

Ben
  • 1,292
  • 9
  • 17

3 Answers3

4

I know this is an old post, but I had the same problem with the new iOS8 UISearchController. The workaround is to check if the string is empty and to set the result tableview's property HIDDEN = NO

Claus
  • 5,662
  • 10
  • 77
  • 118
2

Well the solution to this is to not use the UISearchDisplayController. It simply doesn't offer the flexibility you need to do something like this. Build your own custom search interface with a UISegmentedControl, UISearchBar and UITableView for maximum flexibility.

Ben
  • 1,292
  • 9
  • 17
2

What you can do is When the user clicks on search and the string is empty change the searchBartext to "\n" carriage return

self.searchDisplayController.searchBar.text = @"\n";

The carriage return is not displayed in the searchbar an also it will not be the same string, as if the user types "\n". so it works fine. In the

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 

method you can set the searchbar text back to @"". if the searchstring was @"\n"

self.searchDisplayController.searchBar.text = @"";

That's what worked for me!

kapukinz
  • 165
  • 14
  • whenever I set the self.searchDisplayController.searchBar.text = @""; in the shouldReloadTableForSearchString it doesn't work. I wanted to do that to be able to see my placeholder text. Any thoughts? – DonnaLea Mar 26 '14 at 05:03
  • I must have been half asleep, because it works now. Yay – DonnaLea Mar 26 '14 at 15:11
  • When you have `" "` empty text, placeholder text would be removed. @DonnaLea, could you please explain how it worked for you – Sategroup Sep 13 '15 at 07:16
  • @Sategroup sorry, it's been quite some time since I've touched the codebase I was using this in and no longer have access to it. Sorry I can't shed any further light. – DonnaLea Sep 15 '15 at 04:11