Hide UITableView when displaying searchResultsTableView

I have a UITableView with searchDisplayController . I have

 tableView.backgroundColor = [UIColor clearColor]; 

and

 self.searchDisplayController.searchResultsTableview.backgroundColor = [UIColor clearColor]; 

When I enter text in the search field, the search results are displayed in order, but as the background of the resulting table becomes transparent, I see my tableview , and the table of search results is displayed in tableview . I want to hide the tableview when searchField started editing. I tried

 -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { [tableView setHidden:YES]; [self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; return YES; } 

But it hides the searchBar using tableView. How to fix it?

+6
source share
3 answers

At first I know that you are adding a searchDisplayController to a UITableView .

Remove it. Please add UISearchDisplayController to your View Controller not on UITableView , beacuse if you hide UITableView , then UISearchDisplayController also hide because you added UISearchDisplayController to UITableView .

Thanks:)

+2
source

You can configure the data source for tableView so that it returns 0 sections when the search interface is visible:

 - (NSInteger) numberOfSectionsInTableView: (UITableView *) tableView { if (self.searchDisplayController.active && (tableView != self.searchDisplayController.searchResultsTableview)) return 0; // return 0 for bottom table view if search interface is active else return <your usual number of sections> } 

And instead of hiding your table view, you can do [tableView reloadData] to hide all content. Then, after the search is complete, reload the table view to display the contents again.

Rebooting the table view will reset all the cells in the table view and offset the contents of the table view, so in some cases this may not be very useful.

Alternatively, you can try to iterate through all the visible cells in the table and hide them as follows:

 for (UITableViewCell *cell in tableView.visibleCells) { cell.hidden = YES; } 
+2
source

first make a table socket and connect it to the table then try this line of code

 tableView.hidden=TRUE; 

he will work.

0
source

All Articles