问题
I have a UITableView
with a searchDisplayController
implemented. 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 displaying fine, but as the background of the resultant table is transparent, I am seeing my tableview
and on the tableview
the search result table is displayed. I want to hide the tableView
when the searchField began editing. I have 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 with the tableView. How to fix it?
回答1:
First i know that you add searchDisplayController
on UITableView
.
Remove it Please and Add your UISearchDisplayController
on your View Controller not on UITableView
, beacuse if you hide UITableView
then UISearchDisplayController
also hide because you added UISearchDisplayController on UITableView
.
Thanks :)
回答2:
You can setup the data source for the tableView
in such way 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 then instead of hiding your table view, you could do [tableView reloadData]
to hide all content. Then after search is finished, reload the table view once more to show the content again.
Reloading the table view will reset all the table view cells and the content offset of the table view though, so it may be not a good idea to do that in some cases.
Alternatively, you could try to iterate through all of the table view's visible cells and hide them like that:
for (UITableViewCell *cell in tableView.visibleCells)
{
cell.hidden = YES;
}
回答3:
first of all make the table outlet and connect it with Table then Try this line of code
tableView.hidden=TRUE;
it will work....
来源:https://stackoverflow.com/questions/14807406/hide-uitableview-when-searchresultstableview-displayed