views:

5120

answers:

3

I used the Interface Builder to create a table view, to which I added the library's Search Bar and Search Display Controller to add search functionality. However, IB set it up so that the bar is visible at the top of the screen when the view is first displayed.

I'd like to know how to have the search bar be hidden by default but still scrollable with the table view (see Apple's Mail application for an example). I've tried calling scrollRectToVisible:animated: in viewDidLoad to scroll the table view down, but to no avail. What's the preferred way of hiding the search bar by default?

+9  A: 

First make sure, that the UISearchBar is a child of the UITableView so that it gets scrolled with the table's content and isn't fixed to the top of the view.

The searchbar isn't counted as a row in the tableview, so if you scroll the top of the tableview to the first row, it 'hides' the searchbar:

[yourTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];

Make sure to not scroll the tableview before it contains data (scrollToRowAtIndexPath will raise an exception if the given indexPath does not point to a valid row (i.e. if the tableview is empty)).

Andreas
This works - thanks!
Tim
Actually, I'm getting an issue where the table view won't scroll if there aren't enough rows to fill the screen - in a table with one or two rows, this code does nothing. Is that expected behavior? Can I get around it somehow?
Tim
Sounds like this is normally intended (display everything if fits the screen). The mail app however seems to be able to scroll away the searchbar even if no rows exist. Unfortunately I wasn't able to reproduce that in my own code.
Andreas
I found a way: instead of scrolling to an index path, change the content offset of the table's scroll view to a CGRect at 0.0, 44.0
Tim
Tim, that's the way to go! I used self.tableView.contentOffset = CGPointMake(0, self.searchDisplayController.searchBar.frame.size.height);
Joe D'Andrea
A: 

I find that the "Notes" app is unable to search items when the tableView is blank. So I think it's just using -(void)addSubview:(UIView *)view to add a blank table at first. When you add an item to its data source array, it will run another peice of code to load tableView.

So my solution is:

if([self.arrayList count] > 0)
{
     [self.tableView reloadData];     //use jdandrea's code to scroll view when cell==nil in cellForRowAtIndexPath
     self.tableView.scrollEnabled = YES;
}
else
{
     tableBlank = [[UITableView alloc]initWithFrame:CGRectMake(0,0,320,416) style:UITableViewStylePlain] autorelease];
     tableBlank.delegate = self;
     tableBlank.dataSource = self;
     [self.view addSubview:tableBlank];
}

Hope this helps :-)

iPhoney
A: 

I used the self.tableView.contentOffset = CGPointMake(0, self.searchDisplayController.searchBar.frame.size.height);

This works perfectly when I first get to the UITableViewController (It's pushed onto a navigationcontroller)

but when I hit the backbutton to get to it, it jumps from the correct section to the wrong one

I call self.tableView.contentOffset in the method - (void)viewWillAppear:(BOOL)animated

any idea why it's jumping?

Lucius
Lucius: you should probably post this as a question, rather than an answer to an existing question.
Tim