UISearchDisplayController的基本使用
iOS8以后引入SearchViewController,但是在为了兼容iOS8以前的版本可以使用SearchDisplayViewController
该控制器的展现是一个tableview
使用搜索控制器只需要在原来的控制器中强引用该控制器,点击搜索栏时会自动跳转展现该搜索控制器
- (void)viewDidLoad {
[super viewDidLoad];
self.contentView.delegate = self;
self.contentView.dataSource = self;
// 初始化搜索栏
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
searchBar.backgroundColor = [UIColor colorWithRed:241.0/255.0 green:242.0/255.0 blue:243.0/255.0 alpha:1];
[[[[searchBar.subviews objectAtIndex:0] subviews] objectAtIndex:0] removeFromSuperview];
self.contentView.tableHeaderView = searchBar;
// 初始化搜索控制器 强引用该控制器 设置其代理与数据源
self.searchDisplay = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
self.searchDisplay.searchResultsDelegate = self;
self.searchDisplay.searchResultsDataSource = self;
}
搜索结果展现为tableview。该控制器的代理与数据源与tableviewController的代理和数据源是一样的。所以在代理方法中进行区分显示
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 原tableview
if (tableView == self.contentView) {
return self.subBanks.count;
}else {
// 搜索结果的tableview
// 谓词搜索
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"sbname CONTAINS[cd] %@",self.searchDisplay.searchBar.text];
// 存放搜索结果的数组
self.filterData = [[NSArray alloc] initWithArray:[self.subBanks filteredArrayUsingPredicate:predicate]];
return self.filterData.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
cell.textLabel.textColor = self.subBanks[indexPath.row] == self.selSubBank ? [UIColor redColor] : [UIColor blackColor];
cell.selectionStyle = UITableViewCellEditingStyleNone;
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.numberOfLines = 0;
if (tableView == self.contentView) {
XLSubBankModel *subBank = self.subBanks[indexPath.row];
cell.textLabel.text = subBank.sbname;
}else {
XLSubBankModel *subBank = self.filterData[indexPath.row];
cell.textLabel.text = subBank.sbname;
}
return cell;
}
取消搜索控制器的激活状态
取消激活状态,即和点取消的效果是一样的,返回原来所有数据的界面
[self.searchDisplay setActive:NO animated:YES];
谓词搜索
谓词搜索是搜索控制器中的一种搜索方式
类似于SQL语句的搜索,可以对内容进行更灵活的搜索
示例如下:
Person 类
@interface Person : NSObject
@property NSString *firstName;
@property NSString *lastName;
@property NSNumber *age;
@end
people 为存放 Person对象的数组
// 谓词搜索
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstname CONTAINS[cd] %@",self.searchDisplay.searchBar.text];
// 存放搜索结果的数组
self.filterData = [[NSArray alloc] initWithArray:[self.people filteredArrayUsingPredicate:predicate]];
-
初始化搜索条件
如上面的条件获取 firstname属性中包含搜索框中输入的文本
[cd] 表示不区分大小写 -
在数组中进行过滤
在people数组中进行过滤查找,找出people数组中的所有person对象的firstname包含搜索框中文本的person对象,并将其以数组形式返回
创建不同条件的谓词
- 查找所有age小于输入的年龄的对象
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age < %d",self.searchDisplay.searchBar.text];
- 匹配name属性字符串以a开头的
predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];
- 匹配name属性以ba结尾的
predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];
网友评论