views:

114

answers:

2

Given a UITableViewController with a UISearchBar, how does one change the 'No Results' text that appears in the table view (after any characters are entered) to something like 'Search by Name'? The reason is that the search in question is performed remotely (and has about a second delay), so I can only perform the search when the user selects the search button (not in response to changes to the search criteria). Thus, I still want the 'No Results' text to appear, but only if the user taps the 'Search' button and no results are returned from the server. I am currently have:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    return NO;
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    return NO;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    self.results = [Projects findAllRemote];
    [self.searchDisplayController.searchResultsTableView reloadData];
}
A: 

For sure it's possible. All you have to do is find your "No results" cell, see where its putting that text into some label, and change that labels text, for the cases where no items are found.

That said, I think it's a better idea to leave clear messages for users, so changing the no results text from "No Results" to something less clear, is not generally something I'd recommend.

jer
Rephrased question to 'how do' from 'is possible'. Where is this label stored? This is a default action of the UITableView and UISearchController it appears. I am not specifying this label, and I don't know how to 'find' this label. I still want the 'No Results', but only if a user actually searches and finds no results (i.e. not right after text is entered, but only after the search button is clicked).
Kevin Sylvestre
The label is stored in a cell. If you aren't using a custom cell, then it will just be: `yourCell.label.text = @"foo";` So what you need to do since you clarified more, is initially set the label's text to `@"Search by name"` or whatever, then when you start searching, change that label to read something like `@"Searching..."` and finally, either display your results or if no results were found, `@"No results"`. Hope that clears it up.
jer
How do I gain access to the code that draws this cell? Where does it exist? I am implementing all the UITableViewDataSource methods, and have no clue where it is.
Kevin Sylvestre
in the `tableView:cellForRowAtIndexPath:` delegate method, is where setting up the cells occurs. There is a lot of sample code on this topic built right into Xcode. Open the documentation browser, search for cellForRowAtIndexPath, make sure you click on the UITableView entry not the UITableViewDataSource entry, and then look under sample code for that section—You have 5 items to choose from.
jer
What you recommended does NOT work. I have now identified that the "no results" label corresponds to default behaviour for the UITableView and UISearchBar (while having the no results). It is NOT possible to directly modify the text associated with the label (found similar article here http://stackoverflow.com/questions/1214185/uisearchdisplaycontroller-with-no-results-tableview).
Kevin Sylvestre
I'm doing this in an app right now, so don't tell me it won't work. I build the solution I outlined into a larger class. Only difference is I use custom cells, since I need to display an icon as well as some text.
jer
+1  A: 

From Apple Support Forum: There isn't currently a supported way to change the text (good chance to file a bug!), but you can prevent it from showing up by returning a single row with a blank cell from your data source when you're still waiting for the user to press the search button. As long as there's a cell we won't show the no results text.

Kevin Sylvestre