UISearchDisplayController without tableView results?

As a rule, when activated, the UISearchDisplayController disables the tableView and focuses the SearchBar. Once you enter text in the searchBar, it creates a searchResultsTableView that appears between the SearchBar and the keyboard. The searchDisplayController delegator is called when this second UITableView is loaded / displayed / hidden / unloaded. It usually displays real-time search results or autocomplete entries as you type.

In my application, I want to search the web service, and I do not want to call the web service for every letter the user enters. So I want to completely disable searchResultsTableView and keep the darkened black overlay when it goes into the text. Then I start the search (with the loading screen) when it clicks the search button.

The simple return of null rows for searchResultsTableView does not look beautiful, since it displays an empty SearchResultsTableView with the message "no results". I tried to hide the table when it appears ( searchDisplayController:didLoadSearchResultsTableView: , which works, but the darkened darkened overlay is also hidden, so the underlying tableView will be visible again.

Any ideas other than recreating the functionality of the UISearchDisplayController from scratch?

+42
objective-c iphone uisearchbar
Jul 31 '09 at 18:44
source share
10 answers

here is a little trick I just realized and also you should return 0 results when editing searchstring

 - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { savedSearchTerm = searchString; [controller.searchResultsTableView setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.8]]; [controller.searchResultsTableView setRowHeight:800]; [controller.searchResultsTableView setScrollEnabled:NO]; return NO; } - (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView { // undo the changes above to prevent artefacts reported below by mclin } 

I think you will understand what to do next.

+38
Oct 02 '09 at 0:59
source share

Have you tried this:

 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(_lookup:) object:nil]; [self performSelector:@selector(_lookup:) withObject:txt afterDelay:0.20]; 

That way, if the user enters another char within 1/5 seconds, you only make one web call.

+19
Sep 10 '09 at 16:51
source share

None of the above seemed to work well, so I came up with the following (you should call removeTableHeader when you are ready to display your results):

 - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { [self setTableHeader]; } - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { [self setTableHeader]; } - (void)setTableHeader { UIView *headerView = [[UIView alloc] initWithFrame:self.searchDisplayController.searchResultsTableView.frame]; headerView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8]; [self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor clearColor]]; [self.searchDisplayController.searchResultsTableView setScrollEnabled:NO]; [self.searchDisplayController.searchResultsTableView setTableHeaderView:headerView]; [headerView release]; } - (void)removeTableHeader { [self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor whiteColor]]; [self.searchDisplayController.searchResultsTableView setScrollEnabled:YES]; [self.searchDisplayController.searchResultsTableView setTableHeaderView:nil]; } 

Obviously, it makes the table transparent, adds the table heading to the black / translucent table with the same size as the table, and disables scrolling through the table so that you cannot get higher or behind the heading. As a bonus, you can add something to the title ("please wait ..." or activity indicator).

+9
Sep 03 '11 at 17:00
source share

Had the same problem as you, I handled it with a) , setting the search alpha ResultsTableView to 0 when starting the search, and then b) adding / removing overlayView in the viewController. Works like a charm to me.

 @interface MyViewController() //... @property(nonatomic, retain) UIView *overlayView; //... @end @implementation MyViewController @synthesize overlayView = _overlayView; //... - (void)viewDidLoad { //... //define your overlayView _overlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 480)]; _overlayView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8]; } //hide the searchResultsTableView - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { self.searchDisplayController.searchResultsTableView.alpha = 0.0f; } //when ending the search, hide the overlayView - (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller { [_overlayView removeFromSuperview]; } //depending on what the user has inputed, add or remove the overlayView to the view of the current viewController - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { if ([searchString length]>0) { [self.view addSubview:_overlayView]; } else { [_overlayView removeFromSuperview]; } return NO; } @end 
+9
Jan 30 2018-12-18T00:
source share

it should be enough to implement the following method in your UISearchDisplayDelegate (which is usually your custom subclass of UITableViewController)

 - (BOOL) searchDisplayController: (UISearchDisplayController *) controller shouldReloadTableForSearchString: (NSString *) searchString { [self startMyCustomWebserviceSearchAsBackgroundProcessForString: searchString]; //starts new NSThread return NO; } 

Have you tried this?

+2
Aug 01 '09 at 13:25
source share

Based on user182820 code below is my version. I hide the view of the UISearchDisplayController table. When a character is entered into the search box, I put a β€œdim view”, so it looks like the β€œDull view” of the UISearchDisplayController never left, and then deleted it when the search was completed. If you enter a few characters and click Cancel, the table view will be briefly white, and I don’t know how to get around this.

 - (void)viewDidLoad { ... tableViewMask=[UIView new]; tableViewMask.backgroundColor = [UIColor blackColor]; tableViewMask.alpha = 0.8; } - (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller{ tableViewMask.frame=CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y+controller.searchBar.frame.size.height, self.tableView.frame.size.width, self.tableView.frame.size.height-controller.searchBar.frame.size.height); controller.searchResultsTableView.hidden=YES; } - (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller{ [tableViewMask removeFromSuperview]; } - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{ if (searchString.length==0) [tableViewMask removeFromSuperview]; else [self.tableView addSubview:tableViewMask]; [searchText autorelease]; searchText=[searchString retain]; return NO; } 
+2
Mar 28 '10 at 19:54
source share

How to just make it so simple:

 - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { self.searchDisplayController.searchResultsTableView.hidden=YES; return YES; } 

Works great for me.

+2
Nov 04
source share

I understand better, because there is an error with the "best answer" - the separator and "No results" will be shown when scrolling through the black background table.

 - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { controller.searchResultsTableView.backgroundColor = [UIColor blackColor]; controller.searchResultsTableView.alpha = 0.8; controller.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone; for(UIView *subview in tableView.subviews) { if([subview isKindOfClass:UILabel.class]) { subview.hidden = YES; } } return NO; } - (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar { self.searchDisplayController.searchResultsTableView.backgroundColor = [UIColor whiteColor]; self.searchDisplayController.searchResultsTableView.alpha = 1; self.searchDisplayController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; for(UIView *subview in tableView.subviews) { if([subview isKindOfClass:UILabel.class]) { subview.hidden = NO; } } // search results and reload data .... } 
+1
Aug 17 '10 at 8:49
source share

All existing answers are too complex. You can leave just by hiding the presentation of the results table right away.

 -(void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView { tableView.hidden = YES; } 
+1
Jun 24 '14 at 17:15
source share

I think I found a better implementation for this problem. All of the previous answers correctly show a dim view that is identical to what the UITableView looks like before the search, but for each solution there is not enough functionality to touch the area to cancel the search.

For this reason, I believe this code works better.

First of all, create a BOOL, such as searchButtonTapped, to indicate whether the search button has been clicked. By default it is NO.

Then:

 - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { if (!searchButtonTapped) { // To prevent results from being shown and to show an identical look to how the tableview looks before a search [controller.searchResultsTableView setBackgroundColor:[UIColor clearColor]]; [controller.searchResultsTableView setRowHeight:160]; self.searchDisplayController.searchResultsTableView.scrollEnabled = NO; } else { // Restore original settings [controller.searchResultsTableView setBackgroundColor:[UIColor whiteColor]]; [controller.searchResultsTableView setRowHeight:44]; self.searchDisplayController.searchResultsTableView.scrollEnabled = YES; } return YES; } 

This should now be clear based on the other answers. Remember to also restore the original settings when the user clicks the Search button.

In addition, in the cellForIndexPath method, add:

 cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.contentView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.8]; 

To create the same dim picture that appears before entering text. Make sure that you apply these properties to the right cell, that is, check which UITableView is active and that the user has not clicked the search button.

Then, to a decisive extent, in didSelectRowAtIndexPath:

 if (tableView == self.searchDisplayController.searchResultsTableView) { if (searchButtonTapped) { // Code for when the user select a row after actually having performed a search { else [self.searchDisplayController setActive:NO animated:YES]; 

The user can now click on a dim area that does not result in a visible selection of UITableViewCell, but instead cancels the search.

0
Nov 01 2018-11-11T00:
source share



All Articles