views:

2691

answers:

5

The iPhone SDK 3.0 has this handy new class "UISearchDisplayController" which makes it easy to create a search bar, handling all the user input and displaying the search results.

I am using it for my new app, but there is one thing i would like to change:

As a default, the search bar should be put at the top of the UITableView that displays the search results. So when scrolling down the result list, the search bar disappears.

What I would like to achieve is having the search bar always on top and when scrolling the TableView, the search bar should stay where it is. (Reason for this: in my app there are sometimes a lot of search results, so sometimes the user has to scroll down a while, and when he realizes that he has to change his search string, i don't want to force him to scroll all the way back)

What i already did is adding the search bar to the view of the UINavigationController which is the "parent view" of my table view, instead of adding it to the table view directly:

MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate];

searchBar = [[UISearchBar alloc] init];
searchBar.delegate = self;

[searchBar sizeToFit];
CGFloat searchBarHeight = [searchBar frame].size.height;
CGRect mainViewBounds = delegate.navController.view.bounds;
[searchBar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds),
        CGRectGetMinY(mainViewBounds) + 20,
        CGRectGetWidth(mainViewBounds),
        searchBarHeight)];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;

[delegate.navController.view addSubview: searchBar];

searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar: searchBar contentsController: self];
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;

... but here my problem is, that the table view stays behind the search bar, and there is always the first TableViewCell of the search results, that is hidden behind my search bar.

Is there a way to resize the UITableView of my UITableViewController permanently, so that it starts right under the search bar?

And there's another problem: Everytime i touch the search bar, the UISearchDisplayController automatically hides the Navigation Bar and resizes the UITableView (it expands to the top), so i think that at this point the UISearchDisplayController will have a big problem with my custom sized TableView...

Thanks a lot for any help!!

+2  A: 

I don't have a solution off the top of my head - but I will say the concern you have seems to minimized by the ability for the user to simply tap the top of the screen and have the table zoom to the top (to change search terms if it's wrong).

Kendall Helmstetter Gelner
thanks for your answer! in the meantime i have realized that when entering a search term the UISearchDisplayController automatically locks the location of the searchbar and resizes the result table view... so that is almost exactly what i wanted to achieve...
xoconoxtle
A: 

My general solution for having bits on the page that I don't want to scroll, is to have the UITableView inside another view. That outer view contains two things -- a bar at the top, and a slightly-shorter-than-full-page table below it.

Amagrammer
A: 

@interface:

UISearchDisplayController < UISearchBarDelegate > *searchDisplayController;

@implementation:

searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar: searchBar contentsController: self];

searchDisplayController.delegate = self;
searchBar.delegate = searchDisplayController;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
A: 

in tableviewcontroller implementatin

self.tableview.frame = CGRectMake(0,searchBar.bounds.size.height,320,480);

AM
A: 

its not working for me???

i can see that rect bar but still serach bar is not fixed

ram