UISearchBar 和 UISearchDisplayController的使用
来源:互联网 发布:数据库的主键怎么设置 编辑:程序博客网 时间:2024/05/16 00:59
原文地址:http://hi.baidu.com/happywilma0118/item/e6d5730a499bba1b3a53eef8
UISearchBar继承自UIView、UIResponder、NSObject
属性:
autocapitalizationType————自动对输入文本对象进行大小写设置(包含4种类型,但是有时候键盘会屏蔽此属性)
autocorrectionType————自动对输入文本对象进行纠错。
backgroundImage————searchbar的背景图片,如果图片不是可伸缩的或者1点宽,则一般被平铺
barStyle————控件的样式
delegate————控件的委托,委托要遵从UISearchBarDelegate协议,默认是nil
keyboardType————输入时,键盘的样式
placeholder————半透明的提示文字,输入搜索内容消失
prompt————显示在控件顶部的一行提示文字
text————控件上面的显示的文字
showsBookmarkButton————是否在控件的右端显示一个书的按钮(输入文字时消失)
showsCancelButton————是否显示cancel按钮(默认是显示)
showsSearchResultsButton————是否在控件的右端显示搜索结果按钮
searchResultsButtonSelected————搜索结果按钮是否被选中
tintColor————bar的颜色(具有渐变效果)
translucent————指定控件是否会有透视效果
scopeButtonTitles————搜索栏下部的选择栏,数组里面的内容是按钮的标题
selectedScopeButtonIndex————搜索栏下部的选择栏按钮的个数
showsScopeBar————控制搜索栏下部的选择栏是否显示出来(需设置为YES 才能使用scopebar)
UISearchBar不执行搜索行为,必须使用delegate,当输入搜索文本、点击button按钮后,代理的方法会完成搜索对应的操作。
1.编辑输入事件:
– searchBar:textDidChange:
– searchBar:shouldChangeTextInRange:replacementText:
– searchBarShouldBeginEditing:
– searchBarTextDidBeginEditing:
– searchBarShouldEndEditing:
– searchBarTextDidEndEditing:
2.点击按钮事件:
– searchBarBookmarkButtonClicked:
– searchBarCancelButtonClicked:
– searchBarSearchButtonClicked:
– searchBarResultsListButtonClicked:
3.Scope按钮事件:
– searchBar:selectedScopeButtonIndexDidChange:
利用UISearchBar的委托事件 textDidChange,当在搜索框中输入完成后,如果输入的文本长度>0,可以调用自己的搜索方法,得到搜索结果,然后再reloadData,刷新一下。如果输入文本长度<0,则需要恢复到原始数据。这个方法可以在边输入搜索文本边显示结果。 如果需要按“search”按钮再搜索,则将上述操作放在searchBarSearchButtonClicked中。
利用UISearchDisplayController可以简化很多操作,也能达到搜索的目的。
属性:
active————是搜索界面可视化,默认为no,可用setActive方法设置.
delegate————委托
searchBar————在searchdisplaycontroller初始化后,searchbar是不可修改的,是readonly属性的.
searchContentController————管理搜索内容的试图控制器,一般是一个UITableViewController的实例,意思是针对一个UITableView的内容进行搜索
searchResultsDataSource————搜索结果的数据源
searchResultsDelegate————搜索结果的委托
searchResultsTableView————搜索结果要展示在哪个tableview中(read-only);
searchResultsTitle————搜索结果视图的title
初始化一个searchDisplayController:
UISearchBar * theSearchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width-50, 40)] autorelease];
theSearchBar.placeholder = @"enter province name";
theSearchBar.autocorrectionType = UITextAutocorrectionTypeNo;
theSearchBar.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
theSearchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"All",@"A",@"B",@"C",@"D" ,nil];
theSearchBar.showsScopeBar = YES;
theSearchBar.keyboardType = UIKeyboardTypeNamePhonePad;
theSearchBar.showsBookmarkButton = YES;
tableView.tableHeaderView = theSearchBar; //将searchBar添加到tableView的头,注意滚动出屏幕后,搜索框也不在了,只出现在首页
UISearchDisplayController * searchdispalyCtrl = [[UISearchDisplayController alloc] initWithSearchBar:theSearchBar contentsController:self];
searchdispalyCtrl.active = NO;
searchdispalyCtrl.delegate = self;
searchdispalyCtrl.searchResultsDelegate=self;
searchdispalyCtrl.searchResultsDataSource = self;
使用UISearchDisplayDelegate的委托方法进行搜索操作:
1.搜索状态改变:
– searchDisplayControllerWillBeginSearch:
– searchDisplayControllerDidBeginSearch:
– searchDisplayControllerWillEndSearch:
– searchDisplayControllerDidEndSearch:
– searchDisplayController:didLoadSearchResultsTableView:
– searchDisplayController:willUnloadSearchResultsTableView:
– searchDisplayController:willShowSearchResultsTableView:
– searchDisplayController:didShowSearchResultsTableView:
– searchDisplayController:willHideSearchResultsTableView:
– searchDisplayController:didHideSearchResultsTableView:
– searchDisplayController:shouldReloadTableForSearchString:
– searchDisplayController:shouldReloadTableForSearchScope:
searchDisplayController自身有一个searchResultsTableView,所以在执行操作的时候首先要判断是否是搜索结果的tableView,如果是显示的就是搜索结果的数据,如果不是,是TableView自身的view,则需要显示原始数据。
if(tableView == self.searchDisplayController.searchResultsTableView)
{
arr = [self.filterContent valueForKey:key]; //搜索结果
}
else
{
arr = [self.localresource valueForKey:key]; //原始数据
}
这样就不需要每次都realoadData了。
下文地址:http://blog.csdn.net/uxyheaven/article/details/7881207
之前比較少用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
- 设计模式深入学习IOS版(9)工程依赖解耦之静态链接库
- class-dump获取部分iOS app头文件信息
- PF_RING模块初始化(五)
- dede中上传图片文件失败的解决过程
- C语言之静态链接库与动态链接库(2)
- UISearchBar 和 UISearchDisplayController的使用
- 统计学中数据类型
- 分享一个自己写的JFinal的BaseController (1)
- C#运算符重载
- socket的创建(六)
- 60个数据窗口技巧
- Spring Aop完整实例
- Tomcat 端口号,虚拟目录 设置
- tomcat conf目录下的各xml介绍