如何使用iPhone SDK 3.0中的UISearchDisplayController实现始终位于顶部的搜索栏

问题描述:

iPhone SDK 3.0具有这个方便的新类“UISearchDisplayController”,它可以轻松创建搜索栏,处理所有用户输入并显示搜索结果。如何使用iPhone SDK 3.0中的UISearchDisplayController实现始终位于顶部的搜索栏

我使用它为我的新的应用程序,但有一两件事我想改变:

作为默认的搜索栏应该显示搜索结果的UITableView的顶部放。因此,当向下滚动结果列表时,搜索栏会消失。

我想实现的是将搜索栏始终放在顶部,并且在滚动TableView时,搜索栏应该保持在原来的位置。 (原因是:在我的应用程序中有时候会有很多搜索结果,所以有时用户不得不向下滚动一下,当他意识到他必须更改搜索字符串时,我不想强​​迫他向后滚动一路)

什么我已经做是增加搜索栏到的UINavigationController的观点这是我的表视图的“父视图”,而不是将它添加到直接表视图:

MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate]; 

searchBar = [[UISearchBar alloc] init]; 
searchBar.delegate = self; 

[searchBar sizeToFit]; 
CGFloat searchBarHeight = [searchBar frame].size.height; 
CGRect mainViewBounds = delegate.navController.view.bounds; 
[searchBar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds), 
          CGRectGetMinY(mainViewBounds) + 20, 
          CGRectGetWidth(mainViewBounds), 
          searchBarHeight)]; 
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

[delegate.navController.view addSubview: searchBar]; 

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

...但在这里我的问题是,表视图留在搜索栏后面,总是有搜索结果的第一个TableViewCell,它隐藏在我的搜索栏后面。

有没有办法调整我的UITableViewController的UITableView的永久性,以便它在搜索栏下正确启动?还有另一个问题:每次我触摸搜索栏,UISearchDisplayController自动隐藏导航栏并调整UITableView的大小(它扩展到顶部),所以我认为在这一点上,UISearchDisplayController会有一个大问题我的自定义大小TableView ...

非常感谢任何帮助!

我没有解决我头顶的问题 - 但是我会说您最担心的问题是用户只需轻点屏幕顶部,然后将桌面缩放至顶部(如果它错了,则更改搜索字词)。

+0

谢谢您的回答!在此期间,我意识到,当输入搜索词时,UISearchDisplayController会自动锁定搜索栏的位置并调整结果表视图的大小......这几乎就是我想要实现的内容...... – xoconoxtle 2009-07-31 20:00:18

我不希望滚动页面上的位的一般解决方案是让UITableView 在另一个视图内。这个外部视图包含两件事情 - 顶部是一个条形图,下面是一个略小于整页的表格。

@interface:

UISearchDisplayController <UISearchBarDelegate> *searchDisplayController; 

@implementation:

searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar: searchBar contentsController: self]; 

searchDisplayController.delegate = self; 
searchBar.delegate = searchDisplayController; 
searchDisplayController.searchResultsDataSource = self; 
searchDisplayController.searchResultsDelegate = self; 

将这个代码在你TableViewController:

self.tableView.frame = CGRectMake(0,searchDisplayController?.searchBar.bounds.size.height,320,480);