How to cover "no results" text in a UISearchDisplayController searchResultTableView?

I do not want to display the text "no results" while my server is processing the search request.

enter image description here

I calculated the exact coordinates of the cell of the table that contains the label, and tried to cover it.

self.noResultsCoverView = [[[UIView alloc] initWithFrame:CGRectMake( 0.0, 44.0, 320.0, 43.0 )] autorelease]; self.noResultsCoverView.backgroundColor = [UIColor whiteColor]; [self.searchDisplayController.searchResultsTableView addSubview:self.noResultsCoverView]; 

To my chagrin, my cover was above the table, but below the label. I need the cover to be above the label. searchResultsTableView::bringSubviewToFront not working, which leads me to believe that the label is not a child of searchResultsTableView at all.

By the way, this stack overflow answer doesn't work for me. It works with the very first search, but a strange black cover blinks on subsequent searches.

+5
source share
4 answers

The easiest way to get around this is to return 1 to numberOfRowsInSection at query time and leave the cell empty or set its hidden property to YES so that it is not visible.

+6
source

this should work correctly. Code to return at least one cell:

 BOOL ivarNoResults; // put this somewhere in @interface or at top of @implementation - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView == self.searchDisplayController.searchResultsTableView) { if (filteredList.count == 0) { ivarNoResults = YES; return 1; } else { ivarNoResults = NO; return [filteredList count]; } } // {…} // return the unfiltered array count } 

and to "show" a clean cell:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView == self.searchDisplayController.searchResultsTableView && ivarNoResults) { static NSString *cleanCellIdent = @"cleanCell"; UITableViewCell *ccell = [tableView dequeueReusableCellWithIdentifier:cleanCellIdent]; if (ccell == nil) { ccell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cleanCellIdent] autorelease]; ccell.userInteractionEnabled = NO; } return ccell; } // {…} } 
+20
source

Try it worked for me

In the UISearchDisplayController delegate, do the following: =

 - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.001); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ for (UIView* v in self.searchDisplayController.searchResultsTableView.subviews) { if ([v isKindOfClass: [UILabel class]] && [[(UILabel*)v text] isEqualToString:@"No Results"]) { [(UILabel*)v setText:@""]; break; } } }); return YES; } 
+3
source

You need to understand that when you have a UISearchDisplayController and the search bar is active, the UITableView argument passed to your UITableView data source and the delegation methods are NOT really your tableView object, but the tableView is managed by the UISearchDisplayController designed to display live results Search (for example, results filtered from your main data source, for example).

You can easily find this in the code, and then return the corresponding result from the delegation / data source method, depending on which tableView object is requesting.

For instance:

  - (NSInteger) tableView: (UITableView *) tv numberOfRowsInSection: (NSInteger) section
 {
     if (tv == self.searchDisplayController.searchResultsTableView) {
         // return the number of rows in section for the visible search results.
         // return a non-zero value to suppress "No results"
     } else {
         // return the number of rows in section for your main data source
     }
 }

The fact is that your data sources and delegation methods serve tables two , and you can (and should) check which table is requesting data or delegation.

By the way, "No results" (I think) is provided by the background image that the UISearchDisplayController displays when the delegate says that there are no rows ... You do not see a table of 2 rows, the first space and the second with the text "No results". At least this is what I think is going on there.

+1
source

All Articles