UISearchDisplayController的基本使用
iOS8以后引入SearchViewController位隶,但是在為了兼容iOS8以前的版本可以使用SearchDisplayViewController
該控制器的展現(xiàn)是一個tableview
使用搜索控制器只需要在原來的控制器中強(qiáng)引用該控制器酥宴,點擊搜索欄時會自動跳轉(zhuǎn)展現(xiàn)該搜索控制器
- (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;
// 初始化搜索控制器 強(qiáng)引用該控制器 設(shè)置其代理與數(shù)據(jù)源
self.searchDisplay = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
self.searchDisplay.searchResultsDelegate = self;
self.searchDisplay.searchResultsDataSource = self;
}
搜索結(jié)果展現(xiàn)為tableview皿伺。該控制器的代理與數(shù)據(jù)源與tableviewController的代理和數(shù)據(jù)源是一樣的黄锤。所以在代理方法中進(jìn)行區(qū)分顯示
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 原tableview
if (tableView == self.contentView) {
return self.subBanks.count;
}else {
// 搜索結(jié)果的tableview
// 謂詞搜索
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"sbname CONTAINS[cd] %@",self.searchDisplay.searchBar.text];
// 存放搜索結(jié)果的數(shù)組
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;
}
取消搜索控制器的激活狀態(tài)
取消激活狀態(tài)账磺,即和點取消的效果是一樣的临梗,返回原來所有數(shù)據(jù)的界面
[self.searchDisplay setActive:NO animated:YES];
謂詞搜索
謂詞搜索是搜索控制器中的一種搜索方式
類似于SQL語句的搜索踏揣,可以對內(nèi)容進(jìn)行更靈活的搜索
示例如下:
Person 類
@interface Person : NSObject
@property NSString *firstName;
@property NSString *lastName;
@property NSNumber *age;
@end
people 為存放 Person對象的數(shù)組
// 謂詞搜索
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstname CONTAINS[cd] %@",self.searchDisplay.searchBar.text];
// 存放搜索結(jié)果的數(shù)組
self.filterData = [[NSArray alloc] initWithArray:[self.people filteredArrayUsingPredicate:predicate]];
初始化搜索條件
如上面的條件獲取 firstname屬性中包含搜索框中輸入的文本
[cd] 表示不區(qū)分大小寫在數(shù)組中進(jìn)行過濾
在people數(shù)組中進(jìn)行過濾查找锻全,找出people數(shù)組中的所有person對象的firstname包含搜索框中文本的person對象狂塘,并將其以數(shù)組形式返回
創(chuàng)建不同條件的謂詞
- 查找所有age小于輸入的年齡的對象
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age < %d",self.searchDisplay.searchBar.text];
- 匹配name屬性字符串以a開頭的
predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];
- 匹配name屬性以ba結(jié)尾的
predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];