views:

541

answers:

2

I have placed a UISearchBar in my UITableView.tableHeaderView. However it covers the searchBar by placing the viewable top to the first section header. I can only see the searchBar when I drag the tableView down. It is half covered and then can not be selected because releasing the tableView scrolling will rubber band it back out of view. Please help.

The following is placed in my UITableViewController viewDidLoad method:

UISearchBar *theSearchBar = [[UISearchBar alloc] init];
theSearchBar.delegate = self;
self.searchBar = theSearchBar;
[theSearchBar release];

self.tableView.tableHeaderView = self.searchBar;

The result is the following screenshots: http://imagebin.ca/view/6qNiwHR.html

+1  A: 

I think the tableHeaderView is not the best place to put your search bar. I usually use a UISearchDisplayController:

searchController = [[UISearchDisplayController alloc]
                     initWithSearchBar:theSearchBar contentsController:self];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;

It's pretty straight-forward and give some functions for searching (you have to implement them in the delegate/datasource, in this case your controller).

I usually do it from a nib but i think you just have to assign it to your viewcontroller :

self.searchDisplayController=searchController;

And if it doesn't show the view, you should add the view to the tableView directly.

You can look at the reference, or ask if you have some problems.

Julien
Thank you so much. Didn't even realize there was a controller. Gonna go try this now.
Sukima
At least since 3.0, `searchDisplayController` is a readonly property and cannot be set in this way.
Shaggy Frog
+2  A: 

It turns out that it was a sizing issue. Found a tutorial (http://bit.ly/uOa2r) and it places this in the set up:

[theSearchBar sizeToFit];

which make everything look perfect.

Since UISearchDisplayController uses an already established UISearchBar it doesn't eliminate the problem.

Sukima