之前比較少用UISearchBar 和 UISearchDisplayController,最近閱讀了一些有關資料,簡單做個筆記:
1、UISearchBar 和 UISearchDisplayController在IB中是可以直接使用的,UISearchBar如果不指定delegate及執行相關的方法,那麼與一個TextField類似。加入一個UISearchDisplayController,它會附帶了一個UISearchBar,參考UISearchDisplayController Reference會發現,它其實還有一個searchResultTableView,用於顯示搜尋到的結果。所以,如果要使用UISearchDisplayController時,記得要設定TableView的兩個delegate。
2、以下例子使用代碼創建UISearchBar 和UISearchDisplayController,注意UISearchDisplayController的創建使用了以下的方法:
- (id)initWithSearchBar:(UISearchBar *)searchBar contentsController:(UIViewController *)viewController;
首先要在主ViewController中加入
<UISearchBarDelegate,UISearchDisplayDelegate,UITableViewDelegate,UITableViewDataSource>
並宣告了兩個物件:mySearchBar和 mySearchDisplayController,然後在.m文件中加入如下代碼:
mySearchBar= [[UISearchBaralloc]init];
//可以加入搜索範圍選項scope
[mySearchBarsetScopeButtonTitles:[NSArrayarrayWithObjects:@"First",@"Last",nil]];
mySearchBar.delegate=self;
[mySearchBarsetAutocapitalizationType:UITextAutocapitalizationTypeNone];
[mySearchBarsizeToFit];
//將UISearchBar加入View中,本例加入到一個TableView中,作為其HeadView
//[self.view addSubview:mySearchBar];
self.myTableView.tableHeaderView=mySearchBar;
//本例調整了TableView的大小和位置
self.myTableView.frame=CGRectMake(60,40,260,self.myTableView.frame.size.height);
mySearchDisplayController= [[UISearchDisplayControlleralloc]initWithSearchBar:mySearchBarcontentsController:self];
//以下一句代碼有點令人困惑,試過取消這句代碼,仍能正常運行
[selfsetMySearchDisplayController:mySearchDisplayController];
[mySearchDisplayControllersetDelegate:self];
[mySearchDisplayControllersetSearchResultsDataSource:self];
[mySearchDisplayControllersetSearchResultsDelegate:self];
3、以下加入UISearchBar 和 UISearchDisplayController 的一些delegate作為示範:
#pragma mark UISearchBar and UISearchDisplayController Delegate Methods
-(BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar{
//準備搜尋前,把上面調整的TableView調整回全屏幕的狀態,如果要產生動畫效果,要另外執行animation代碼
self.myTableView.frame=CGRectMake(0,0,320,self.myTableView.frame.size.height);
returnYES;
}
-(BOOL)searchBarShouldEndEditing:(UISearchBar*)searchBar{
//搜尋結束後,恢復原狀,如果要產生動畫效果,要另外執行animation代碼
self.myTableView.frame=CGRectMake(60,40,260,self.myTableView.frame.size.height);
returnYES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController*)controller
shouldReloadTableForSearchString:(NSString*)searchString
{
//一旦SearchBar輸入內容有變化,則執行這個方法,詢問要不要重裝searchResultTableView的數據
[selffilterContentForSearchText:searchStringscope:
[[self.searchDisplayController.searchBarscopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBarselectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
returnYES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController*)controller
shouldReloadTableForSearchScope:(NSInteger)searchOption
{
//一旦Scope Button有變化,則執行這個方法,詢問要不要重裝searchResultTableView的數據
[selffilterContentForSearchText:[self.searchDisplayController.searchBartext]scope:
[[self.searchDisplayController.searchBarscopeButtonTitles]
objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
returnYES;
}
分享到:
相关推荐
RoySearch//把UISearchBar和UISearchDisplayController封装在一起的一个Search控件,用时只需在需要调用的ViewController里调用即可。完美适配tableView。//A customed and packaged category that combined ...
在UINavigationBar上显示UISearchBar, 并实现UISearchDisplayController功能的小Demo, 更多讲解在http://www.cnblogs.com/sely-ios/p/Sely.html
通过 fmdb 进行的数据库的 基本操作(增删改查 )查找是使用 UISearchBar 和UISearchDisplayController 进行混合使用
UISearchBar+UISearchDisplayController,完整例子。
TableSearch 示例项目使用了 UISearchBar 和 UISearchDisplayController,在情节提要中配置。 目前似乎无法在 Interface Builder 中配置 UISearchController。 因此,它是在每个示例中的代码中创建和
Xcode6 storyboard 输入几月几日可得 星座 输入首字母可得 地名 搜索,索引 UISearchBar UISearchDisplayController
UISearchController 内部自带 searchBar 等价于 UISearchDisplayController + UISearchBar
这个源码案例是一个UISearchBar Demo搜索,源码SearchBarDemo,简单演示一下UISearchDisplayController怎么使用。 测试环境:Xcode 6.2,iOS 6.0 以上
UISearchDisplayController适配 ##Description 本Demo的搜索栏UISearchBar脱离UITableView的tableHeaderView而存在,做了对iOS7,8的适配 ##效果图 ##测试环境 Xcode 6.4,模拟器iPhone5,5s,6,6 plus,真机5 iOS8.1
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{ // 谓词的包含语法 NSPredicate *preicate = [NSPredicate ...