2014-09-10 30 views
1

我有一个定制的UISearchBar,我想在self.navigationItem.titleView显示。我没有问题,得到它在那里,看着我希望它的方式,但我认为有些事情是错的我如何与UISearchDisplayController连接。定制的UISearchBar在navigationItem ios7

这里是的UISearchBar和UISearchDisplayController在viewDidLoad中:

searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0,0.0,200.0, 22.0)]; 
searchBar.barStyle = UISearchBarStyleDefault; 
searchBar.showsCancelButton = NO; 
searchBar.placeholder = @"Search"; 
searchBar.delegate = self; 
self.navigationItem.titleView = searchBar; 

searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; 
searchDisplayController.delegate = self; 
searchDisplayController.searchResultsDelegate = self; 
searchDisplayController.searchResultsDataSource = self; 

此时的搜索功能是不实际工作。如果我添加[searchDisplayController setDisplaysSearchBarInNavigationBar:YES];搜索功能,现在的作品,但它弄乱了,我已经加入到searchBar所有样式我。

编辑:附加信息:貌似[self.searchDisplayController.searchResultsTableView reloadData];是没有得到调用。我有以下实现的:

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 

出于某种原因,如果我不叫[searchDisplayController setDisplaysSearchBarInNavigationBar:YES];然后将数据不会被重新装入。也许我错过了一种方法。

在寻找它看起来像- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section被调用和tableView == self.searchDisplayController.searchResultsTableView是真实的,但它不进行调用- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

+0

所以这里是为什么'setDisplaysSearchBarInNavigationBar'不按我想要的方式工作,它显式覆盖原始导航栏:当您返回YES以在导航栏中显示搜索栏时,系统使用搜索显示控制器的navigationItem属性并忽略searchContentsController视图控制器的导航项(如果已设置)。显示的搜索字段在导航栏中占据尽可能多的宽度。 – MCH 2014-09-11 13:36:17

+0

我可以做,如果我创建一个新的查看它的工作,添加搜索栏作为一个子视图,设定titleview的新观点,并设置contentsController到self.navigationController,但新的观点完全打乱了搜索栏的显示。 – MCH 2014-09-11 16:19:43

回答

1

嗯,我想获得它的工作的唯一办法是嵌入的UISearchBar到一个UIView,我尽管我已经能够使它完全一样,但现在的风格还可以。