Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide UITableView when searchResultsTableView displayed

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?

like image 618
tausun Avatar asked Feb 11 '13 07:02

tausun


People also ask

How to hide section in UITableView?

You can't "hide" a section as such, but you can "delete" it from the table view using the deleteSections:withRowAnimation: method. This will remove it from the view, with an optional animation, without affecting your backing data. (You should, however, update the data anyway so that the section doesn't reappear.)

How to hide UITableView header in swift?

I also wanted to hide the headerView. To do this, I did the following: Set up your tableView as per the excellent answer @sasquatch gave above. In the numberOfRowsInSection(section: Int) and tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) functions, check whether the rows/height should be 0.

Is UITableView scrollable?

UITableView scrolls back because it's content size is equal to it's frame (or near to it). If you want to scroll it without returning you need add more cells: table view content size will be large then it's frame.


2 Answers

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 :)

like image 148
iPatel Avatar answered Sep 28 '22 09:09

iPatel


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;
}
like image 26
Egor Chiglintsev Avatar answered Sep 28 '22 09:09

Egor Chiglintsev