3

UITableViewのUISearchBarとUISearchDisplayControllerをプログラムで作成するのに問題があります。UISearchBarとUISearchDisplayControllerを設定してから、self.tableview.tableHeaderViewをself.searchBarに設定しました。tableHeaderViewはまだNULLですが、searchBarとsearchDisplayControllerはNULLではありません。UISearchBarをnullにせず、テーブルヘッダーに表示するにはどうすればよいですか?

これは私が設定したものです:

- (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];
}

そして、私のすべてのUITableViewDelegateメソッドには、検索テーブルにデータを入力するためにこれが含まれています。

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

私はこれを正しく設定したように感じます、誰かが私を助けてくれますか?

4

1 に答える 1