3
votes

I'm having trouble programmatically creating a UISearchBar and UISearchDisplayController for my UITableView. I've set up my UISearchBar and UISearchDisplayController and then set the self.tableview.tableHeaderView to self.searchBar. The tableHeaderView is still NULL but the searchBar and the searchDisplayController are not. How do I get the UISearchBar to not be null and appear in the table header?

This is what I have set up:

- (void)viewDidLoad {
[super viewDidLoad];

/*other things here*/

//Search Bar
self.searchedDancerData = [[NSMutableArray alloc] init];
self.searchBar = [[UISearchBar alloc] init];
self.searchBar.delegate = self;
[self.searchBar sizeToFit];
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
[self.tableView setTableHeaderView:self.searchBar];
self.tableView.tableHeaderView.frame = CGRectMake(0, 0, tableView.frame.size.width, 44);

self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
[self.searchDisplayController setDelegate:self];
[self.searchDisplayController setSearchResultsDataSource:self];
[self.tableView reloadData];
NSLog(@"%@", self.tableView.tableHeaderView); //Returns NULL
}

- (void)viewWillAppear:(BOOL)animated
{
/*other things*/

//set Content offest
[self.tableView setContentOffset:CGPointMake(0, 44)]; 
}

#pragma mark UISearchDisplayControllerDelegate

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self.searchedDancerData removeAllObjects];

for (NSDictionary *dancer in self.dancerData) {
    NSRange range = [[dancer objectForKey:@"Name"] rangeOfString:searchString options:NSCaseInsensitiveSearch];
    if (range.location != NSNotFound) {
        [self.searchedDancerData addObject:[dancer copy]];
        [self.searchedDancerData sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    }
}
return YES;
}

- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
[self.searchDisplayController.searchResultsTableView setDelegate:self];
}

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
[self.tableView setContentOffset:CGPointMake(0, 44) animated:YES];
}

-(void)searchBar:(id)sender
{
[self.searchDisplayController setActive:YES animated:YES];
}

And all of my UITableViewDelegate methods include this to populate the search table:

if (self.searchDisplayController.searchResultsTableView == theTableView) {
    return something;
} else {
    return somethingElse
}

I feel like I've set this UP correctly, can someone help me out please.

1
maybe try to set the frame of uisearchbar ? - ewiinnnnn
Nope, didn't work. Also I tried adding a search button to the navigationBar, and when I click it the searchDisplayController works, but the searchBar still does not appear. - Brandon Mcq

1 Answers

2
votes

Try setting the searchBar as UISearchDisplayController's SearchBar in your tableView's header view.

[self.tableView setTableHeaderView:self.searchDisplayController.searchBar];