2012-06-22 3 views
3

프로그래밍 방식으로 UITableView에 대한 UISearchBar 및 UISearchDisplayController를 만드는 데 문제가 있습니다. 내 UISearchBar 및 UISearchDisplayController를 설정 한 다음 self.tableview.tableHeaderView를 self.searchBar로 설정합니다. tableHeaderView는 여전히 NULL이지만 searchBar 및 searchDisplayController는 없습니다. UISearchBar가 null이 아니고 테이블 헤더에 나타나게하려면 어떻게해야합니까?UISearchBar이 UITableView.tableHeaderView로 설정되지 않음

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

내가 제대로이 설정 한 같은 느낌 :

- (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 방법의 모든이가 검색 테이블을 채우기 위해 다음과 같습니다

내가 설정 한 것입니다 , 누군가 나를 도와주세요.

+0

아마도 uisearchbar 프레임을 설정하려고합니까? – ewiinnnnn

+0

아니, 작동하지 않았다. 또한 navigationBar에 검색 버튼을 추가하려고 시도했는데 searchDisplayController를 클릭해도 searchBar가 나타나지 않습니다. –

답변

2

tableView의 헤더보기에서 searchBar를 UISearchDisplayController의 SearchBar로 설정해보십시오.

[self.tableView setTableHeaderView:self.searchDisplayController.searchBar]; 
관련 문제