I've two issues:
- I have a search bar in my iPhone screen. When I scroll my screen then my searchbar also gets scrolled. I want to fix this or make it absolute.
- Searching is not working like If I enter A then I should get all words that start with alphabet A.
Code that I'm trying:
- (void)viewDidLoad {
[super viewDidLoad];
const NSInteger searchBarHeight = 45;
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0,
self.tableView.frame.size.width, searchBarHeight)];
searchBar.delegate = self;
self.tableView.tableHeaderView = searchBar;
[searchBar release];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Refresh"
style:UIBarButtonItemStyleBordered target:self action:@selector(onAddContact:)];
self.navigationItem.rightBarButtonItem = addButton;
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor]; // change this color
self.navigationItem.titleView = label;
label.text = NSLocalizedString(@"All Contacts", @"");
[label sizeToFit];
content = [DataGenerator wordsFromLetters];
indices = [[content valueForKey:@"headerTitle"] retain];
}
Here when you scroll the
UITableView
at that time alsoUISearchBar
scrolled because you addUISearchBar
as a headerview ofUITableView
see this line..so its scroll with
UITableView
, here for fix theUISearchBar
just add as a subview ofself.view
with frame but outside fromUITableView
For Example..For more information, See bellow links with tutorials and demo for
UISearchBar
withUITableView
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
MethodYou need to separate the Search bar from Table View.Just add searchBar into view rather add it into table. The code is as follows:
then add the table view.
For UISearchDisplayController example, please follow this tutorial: http://cocoabob.net/?p=67 or http://blog.mugunthkumar.com/coding/iphone-tutorial-uisearchdisplaycontroller-with-nspredicate/
Source code are also available. If you don't understand, please let me know. I have a project where all of these have already implemented.