1.UISearchBar(效果如下:)
①创建UISearchBar对象
//初始化,定义frame
UISearchBar *bar = [[UISearchBar alloc] initWithFrame:CGRectMake
(0, 50, self.view.frame.size.width, 80)];
//添加到控制器的视图上
[self.view addSubview:bar];②UISerachBar的属性
//autocapitalizationType:包含4种类型,但是有时候键盘会屏蔽此属.
//1.autocapitalizationType————自动对输入文本对象进行大小写设置.
bar.autocapitalizationType = UITextAutocapitalizationTypeWords;
//2.autocorrectionType————自动对输入文本对象进行纠错
bar.autocorrectionType = UITextAutocorrectionTypeYes;
//3.设置title
bar.prompt = @"全部联系人";
//4.设置颜色
bar.tintColor = [UIColor purpleColor];//渲染颜色
bar.barTintColor = [UIColor orangeColor];//搜索条颜色
bar.backgroundColor = [UIColor purpleColor];//背景颜色,因为毛玻璃效果(transulent).
//5.translucent————指定控件是否会有透视效果
bar.translucent = YES;
//6.scopeButtonTitles(范围buttonTitle)
bar.scopeButtonTitles = @[@"精确搜索",@"模糊搜索"];
bar.selectedScopeButtonIndex = 1;//通过下标指定默认选择的那个选择栏
//7.控制搜索栏下部的选择栏是否显示出来(需设置为YES 才能使用scopebar)
bar.showScopeBar = YES;
//8.设置搜索栏右边的按钮
bar.showsSearchResultsButton = YES;//向下的箭头
bar.showsCancelButton = YES; //取消按钮
bar.showsBookmarkButton = YES; //书签按钮
//9.提示内容
bar.placeholder = @"搜索";
//10.取消键盘操作
[searchBar resignFirstResponder];
//11.设置代理
//UISearchBar不执行搜索行为,必须使用delegate,当输入搜索文本、点击button按钮后,代理的方法
会完成搜索对应的操作。
//.控件的委托,委托要遵从UISearchBarDelegate协议,默认是nil
bar.delegate = self;
1).输入编辑事件处理
– searchBar:textDidChange: – searchBar:shouldChangeTextInRange:replacementText: – searchBarShouldBeginEditing: – searchBarTextDidBeginEditing: – searchBarShouldEndEditing: – searchBarTextDidEndEditing:2).点击按钮事件处理
– searchBarBookmarkButtonClicked: – searchBarCancelButtonClicked: – searchBarSearchButtonClicked: – searchBarResultsListButtonClicked:3).点击Scope按钮事件处理
– searchBar:selectedScopeButtonIndexDidChange:</span>
2.UISearchDisplayController(注:iOS8以上已经弃用)
结合UISearchBar实现效果如下,实现搜索功能.
提示:检测Xcode系统版本代码如下:
[[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 ? YES: NO;①.创建对象
//需要创建UISearchBar对象,这里将对象都定义成了属性
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
//设置UISearchBar属性,上面有UISearchBar详细介绍.
self.searchBar.placeholder = @"enter province name";
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
self.searchBar.scopeButtonTitles = [NSArray arrayWithObjects:
@"All",@"A",@"B",@"C",@"D" ,nil];
self.searchBar.showsScopeBar = YES;
self.searchBar.keyboardType = UIKeyboardTypeNamePhonePad;
self.searchBar.showsBookmarkButton = YES;
//将seachBar作为控制器的透视图,视图控制器,继承UITableViewController
self.tableView.tableHeaderView = _searchBar;
//将UIsearchBar添加到UIdSearchDispalyController上
self.displayController = [[UISearchDisplayController alloc]
initWithSearchBar:_searchBar contentsController:self];注:searchBar————在searchdisplaycontroller初始化后,searchbar是不可修改的,是readonly属性的.
②配置UISearchDisplayController的属性
//active————是搜索界面可视化,默认为no,可用setActive方法设置.
self.displayController.active = YES;
// searchResultsDataSource————搜索结果的数据源,代理对象(UITableViewDataSource)
self.displayController.searchResultsDataSource = self;
//searchResultsDelegate————搜索结果的委托,服从协议UITableViewDelegate
self.displayController.searchResultsDelegate = self;③实现 /*searchDisplayController 自身有一个searchResultsTableView,
所以在执行操作的时候首先要判断是否是搜索结果的tableView,如果是显示的就是搜索结果的数据,
如果不是,是TableView自身的view,则需要显示原始数据。*/
if (tableView == self.tableView) {
return self.dataArray.count;
} else {
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"self contains [cd] %@",self.searchBar.text];
self.arr = [[NSMutableArray alloc] initWithArray:
[self.dataArray filteredArrayUsingPredicate:predicate]];
return self.arr.count;
}
④使用UISearchDisplayDelegate的委托方法进行搜索操作:
1).搜索状态改变事件处理方法:
– searchDisplayControllerWillBeginSearch: – searchDisplayControllerDidBeginSearch: – searchDisplayControllerWillEndSearch: – searchDisplayControllerDidEndSearch:
2).装载和卸载tableview事件处理方法:
– searchDisplayController:didLoadSearchResultsTableView: – searchDisplayController:willUnloadSearchResultsTableView:
3).显示和隐藏tableview事件处理方法:
– searchDisplayController:willShowSearchResultsTableView: – searchDisplayController:didShowSearchResultsTableView: – searchDisplayController:willHideSearchResultsTableView: – searchDisplayController:didHideSearchResultsTableView:4).搜索条件改变时响应事件处理方法:
– searchDisplayController:shouldReloadTableForSearchString: – searchDisplayController:shouldReloadTableForSearchScope
3.UISearchController(iOS8新特性)
UISearchController实现和上述效果基本一致,适用于iOS8以上版本
实现如下图搜索效果
代码如下:
1)新建控制器,继承与UITableViewController,在extension中定义属性
//存储原来的数据 @property (nonatomic, retain) NSArray *dataArr; //存储检索后的数据 @property (nonatomic, retain) NSArray *arr;2).加载数据,懒加载
- (NSArray *)dataArr {
if (!_dataArr) {
self.dataArr = [NSArray arrayWithObjects:
@"Allan",@"Abbbb",@"Acccc",@"Bccccc",@"Cddddffk",@"Cddkllll",
@"Ekkflfl",@"Ekljljfg" ,@"Leslie",@"Mm",@"Meinv",@"Meihi",@"Catilin",
@"Arron", @"211", @"232", @"243", @"264", @"285", @"106", @"311",
@"432", @"543", @"664", @"785", @"806", nil];
}
return _dataArr;
}
//如果检索后的数据为空,将原有数据赋值给检索数据
- (NSArray *)arr {
if (!_arr) {
self.arr = self.dataArr;
}
return _arr;
}
3.加载UISearchController对象- (void)viewDidLoad {
[super viewDidLoad];
//cell重用机制,调用系统的
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:@"lock"];
//创建搜索条,将自身设置为展示结果的控制器
UISearchController *searchVC =[[UISearchController alloc]
initWithSearchResultsController:nil];
//设置渲染颜色
searchVC.searchBar.tintColor = [UIColor orangeColor];
//设置状态条颜色
searchVC.searchBar.barTintColor = [UIColor orangeColor];
//设置开始搜索时背景显示与否(很重要)
searchVC.dimsBackgroundDuringPresentation = NO;
//适应整个屏幕
[searchVC.searchBar sizeToFit];
//设置显示搜索结果的控制器
searchVC.searchResultsUpdater = self; //协议(UISearchResultsUpdating)
//将搜索控制器的搜索条设置为页眉视图
self.tableView.tableHeaderView = searchVC.searchBar;
}
4).实现协议中的方法,必须实现
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
//谓词检测
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"self contains [cd] %@", searchController.searchBar.text];
//将所有和搜索有关的内容存储到arr数组
self.arr = [NSMutableArray arrayWithArray:
[self.dataArr filteredArrayUsingPredicate:predicate]];
//重新加载数据
[self.tableView reloadData];
}
5).设置UITabelViewController中的其它
#pragma mark - Table view data source
//设置有多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
//每个分区有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section {
return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
self.cell = [tableView dequeueReusableCellWithIdentifier:@"lock"
forIndexPath:indexPath];
//设置cell上展示的内容,即搜索后的数据
self.cell.textLabel.text = self.arr[indexPath.row];
return self.cell;
}注.以上是实现搜索框搜索空能.(当搜索内容为空时,返回的时所有数据,如果搜索内容为空,返回空时,需要进行其它修改操作.)
【学习ios之路:UI系列】(UISearchBar,UISearchDisplayController) 和UISearchController(iOS8新特性)
原文:http://blog.csdn.net/zfx5130/article/details/43371403