I'm new to ios, but I couldn't find the answer anywhere.
I got a UITableView showing products. In the HeaderView I got a UISearchBar. The idea is that with the SearchBar you can filter the rows in the TableView. I don't want to / can't use the "TableView" the SearchBar lays on top, so I hid it with searchDisplayController.searchResultsTableView.hidden = YES;. This works finde, the rows get filtered and the user can click on a desired element.
The Problem is that my TableView isn't scrollable anymore! How can I avoid this? I set self.tableView.scrollEnabled = YES; in searchBarTextDidBeginEditing.
visual demonstration: http://d.pr/i/vqiK
Thanks for your help.
How I added the SearchBar to the TableView:
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, SEARCHBAR_WIDTH_EXPAND, SEARCHBAR_HEIGHT)];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
self.tableView.tableHeaderView = searchBar;
searchBar.delegate = self;
Related
How is a searchbar added above a UITableView? Would just the searchbar be enough or would the search bar and search display be chosen?
I want to redraw the tableview with the items that are found by the search.
UISearchDisplayController is specifically designed to solve your problem. You should be able to figure out how to use it from reading the documentation.
Of course you could just handle the UISearchBar (or even UITextField if you want to build your own search bar) yourself.
Here's some code to get you started:
- (void)viewDidLoad {
[super viewDidLoad];
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchBar.delegate = self;
self.tableView.tableHeaderView = searchBar;
searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
searchController.delegate = self;
}
Pretty much all there's left to do is to implement the delegates. If you need any help with that let me know, but may I suggest you ask a new question for each problem you encounter a long the way. Of course if you leave a comment here I will take a look at it.
I have a UIViewController which has a grouped UITableView as a property. I instantiate the UITableView in code and don't use IB. I would like to hook up a UISearchDisplayController to it but can't find any example how this could be done.
This what I have.
//Have implemented the UISearchDisplayDelegate in the header file
//SearchBar
UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 45)];
searchBar.barStyle=UIBarStyleBlackTranslucent;
searchBar.showsCancelButton=NO;
searchBar.autocorrectionType=UITextAutocorrectionTypeNo;
searchBar.autocapitalizationType=UITextAutocapitalizationTypeNone;
searchBar.delegate=self;
UISearchDisplayController *mySearchDisplayController = [[UISearchDisplayController alloc ]initWithSearchBar:searchBar contentsController:self];
self.searchDisplayController = mySearchDisplayController; //Error here ?? self.searchDisplayController is ReadOnly and can't assign
[self.searchDisplayController setDelegate:self];
[self.searchDisplayController setSearchResultsDataSource:self];
[mySearchDisplayController release];
[myDisplayController release];
This doesn't seem to work, the searchDisplayController propery of the UIViewController seems to be readonly and I can't hook myDisplayController onto it. I'm really not sure if this the right way to do it.
I've been looking all around google to find some tip or tutorial on how to use a UISearchDisplayController in UIViewController. All the examples I could find was how to implement it into UITableViewController using IB, which is not the way I want to use it.
Can anyone explain how I could get this working ?
Here's the code that I use. Put this in viewDidLoad of a UIViewController that instantiates it's own UITableView. I think the part you're looking for is to add the search bar as the header view of the table view.
UISearchBar * theSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,40)]; // frame has no effect.
theSearchBar.delegate = self;
if ( !searchBarPlaceHolder ) {
searchBarPlaceHolder = #"What are you looking for?";
}
theSearchBar.placeholder = searchBarPlaceHolder;
theSearchBar.showsCancelButton = YES;
self.theTableView.tableHeaderView = theSearchBar;
UISearchDisplayController *searchCon = [[UISearchDisplayController alloc]
initWithSearchBar:theSearchBar
contentsController:self ];
self.searchController = searchCon;
[searchCon release];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
[searchController setActive:YES animated:YES];
[theSearchBar becomeFirstResponder];
See the Apple Docs:
#property(nonatomic, readonly, retain) UISearchDisplayController *searchDisplayController
Discussion: This property reflects the value of the
searchDisplayController outlet that you set in Interface Builder. If
you create your search display controller programmatically, this
property is set automatically by the search display controller when
it is initialized.
Looks like dumb question, but...:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.bar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 20.0, self.navigationController.view.bounds.size.width, HEADER_HEIGHT)];
mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:bar contentsController:self];
mySearchDisplayController.delegate = self;
mySearchDisplayController.searchResultsDataSource = self;
mySearchDisplayController.searchResultsDelegate = self;
[self.navigationController.view addSubview:self.bar];
All behavior is working fine while i don't press a cancel or hided tableview.
After that search bar is closed and i see a clear navigation bar, without search bar.
I has change to viewDidload, but nothing changed.
Before start printing:
after:
Try to add searchbar as titleView for navigationBar. It may work.
Refer to this site:
http://iphonesdevsdk.blogspot.com/2011/04/custom-tableview.html
It may helps you
In my view controller, when I instantiate my UISearchDisplayController object, I set its various properties, including the background color of the searchResultsTableView property:
self.searchDisplayController = [[[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self] autorelease];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
UIView *tableViewHeaderBackground = [[UIView alloc] initWithFrame:CGRectMake(0,-480,480,480)];
tableViewHeaderBackground.backgroundColor = [UIAppDelegate defaultBackgroundTint];
[searchDisplayController.searchResultsTableView addSubview:tableViewHeaderBackground];
[tableViewHeaderBackground release];
I use this approach for coloring the space above the search bar when I am in the "normal" non-search table view, and this works properly there.
However, the tableViewHeaderBackground view does not appear to be a subview of this search display controller's table view. I do not see the defaultBackgroundTint that I applied to this subview.
Is there a correct way to add a background tint to the "outside" of the search results table view?
Hi: I want to display a section index in an UITableView with a search bar in the table view header (not section header).
But the index strip is now overlapping the search bar. Is there an elegant solution to avoid this and let the index start below the table header?
I realize this answer comes very late, but I had the same problem and searched here and elsewhere. I found good answers here:
Changing the size of the UISearchBar TextField?
There are several suggestions, but the one I found most straightforward was to put a UISearchBar and UINavBar in a UIView and pass that view to setTableView. The problem is that the table controls the size of the header view, ignoring any setting you add. So adding the view makes the table happy and you can reach inside and set the search bar size without being interfered with by the table.The UINavBar has the same background styles as the UISearchBar, so fills the extra space in a visually consistent way, though you have to tweak its frame - See below:
UISearchBar searchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0, 1, 290, 40)] autorelease];
UIView *searchView = [[[UIView alloc] initWithFrame:CGRectMake(0, 1, 320, 40)] autorelease];
UINavigationBar *navBar = [[[UINavigationBar alloc] initWithFrame:CGRectMake(1, -1, 320, 41)] autorelease]; // same gradient/style as search bar
[navBar setTintColor:[UIColor lightGrayColor]];
[searchView addSubview:navBar];
[searchView addSubview:searchBar];
I add empty strings to indexes array:
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSMutableArray *array = [NSMutableArray arrayWithArray:_sortedKeys];
[array insertObject:#"" atIndex:0];
[array insertObject:#"" atIndex:0];
return array;
}