UISearchBar 和 UISearchDisplayController的使用
来源:互联网 发布:怎样做文明网络小公民 编辑:程序博客网 时间:2024/06/05 07:44
之前比較少用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 = [[UISearchBar alloc] init];
//可以加入搜索範圍選項scope
[mySearchBar setScopeButtonTitles:[NSArray arrayWithObjects:@"First",@"Last",nil]];
mySearchBar.delegate = self;
[mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[mySearchBar sizeToFit];
//將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 = [[UISearchDisplayController alloc] initWithSearchBar:mySearchBarcontentsController:self];
//以下一句代碼有點令人困惑,試過取消這句代碼,仍能正常運行
[self setMySearchDisplayController:mySearchDisplayController];
[mySearchDisplayController setDelegate:self];
[mySearchDisplayController setSearchResultsDataSource:self];
[mySearchDisplayController setSearchResultsDelegate: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);
return YES;
}
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
//搜尋結束後,恢復原狀,如果要產生動畫效果,要另外執行animation代碼
self.myTableView.frame = CGRectMake(60, 40, 260, self.myTableView.frame.size.height);
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
//一旦SearchBar輸入內容有變化,則執行這個方法,詢問要不要重裝searchResultTableView的數據
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchScope:(NSInteger)searchOption
{
//一旦Scope Button有變化,則執行這個方法,詢問要不要重裝searchResultTableView的數據
[self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:
[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- UISearchBar和 UISearchDisplayController的使用
- UISearchBar和 UISearchDisplayController的使用
- UISearchBar 和 UISearchDisplayController的使用
- UISearchBar 和 UISearchDisplayController的使用
- UISearchBar和 UISearchDisplayController的使用
- UISearchBar 和 UISearchDisplayController的使用
- UISearchBar 和 UISearchDisplayController的使用
- UISearchBar和 UISearchDisplayController的使用
- UISearchBar 和 UISearchDisplayController的使用
- UISearchBar 和 UISearchDisplayController的使用
- UISearchBar和 UISearchDisplayController的使用
- UISearchBar 和 UISearchDisplayController的使用
- UISearchBar 和 UISearchDisplayController的使用
- iOS UISearchBar 和 UISearchDisplayController的使用
- 关于UISearchDisplayController和 uisearchBar使用时遇到的问题
- IOS UISearchBar + UISearchDisplayController使用
- UISearchBar和UISearchDisplayController
- UISearchBar和UISearchDisplayController
- LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- 连接mysql数据库时出现1045错误的解决方法。
- Java基础--面向对象
- 北漂-开始的日子
- 开发工具集锦
- UISearchBar 和 UISearchDisplayController的使用
- 基于HALCON的模板匹配方法总结
- JAVA的String 类【转】
- Deep Learning(深度学习)学习笔记整理(一)
- Boost::Any中的两项技术,非模板类的模板构造函数和模板类的非模板基类
- 笔记_网络03
- Spring @Transactional (一) 加强版
- slf4j相关的配置信息
- Deep Learning(深度学习)学习笔记整理(二)