iOS 8.0以后推出的新类UISearchController用来替代UISearchBar+UISearchDisplayController,如果想偷懒可以尝试下。
项目地址:
https://github.com/MisterZhouZhou/UISearchControllerAndUILocalizedCollection
现根据UISearchController的初始化
[UISearchController alloc] initWithSearchResultsController:xx]
进行两种形式的体验。
1、initWithSearchResultsController:nil
1.1 初始化UISearchController:
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchController.searchResultsUpdater = self;
_searchController.searchBar.placeholder = @"";
//设置cancel 为取消
[_searchController.searchBar setValue:@"取消" forKey:@"_cancelButtonText"];
//是否设置半透明覆盖层(在initWithSearchResultsController:nil的情况下最好设置为NO)
_searchController.dimsBackgroundDuringPresentation = NO;
_searchController.searchBar.delegate = self;
[_searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = _searchController.searchBar;
1.2 设置数据源与显示:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (self.searchController.active){
return self.filteredArray.count;
}
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
if (self.searchController.active){
cell.textLabel.text = self.filteredArray[indexPath.row];
}
else{
cell.textLabel.text = self.dataArray[indexPath.row];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.searchController.active){
NSLog(@"%@",self.filteredArray[indexPath.row]);
}
else{
NSLog(@"%@",self.dataArray[indexPath.row]);
}
}
1.3 数据筛选:
#pragma mark - UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString * searchString = searchController.searchBar.text;
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",searchString];
self.filteredArray = [[self.dataArray filteredArrayUsingPredicate:predicate] mutableCopy];
[self.tableView reloadData];
}
2、initWithSearchResultsController:vc
2.1 初始化:
- (void)configureSearchController {
_resultVC = [SearchResultViewController new];
_searchController = [[UISearchController alloc] initWithSearchResultsController:_resultVC];
_searchController.searchResultsUpdater = self;
_searchController.searchBar.placeholder = @"";
//设置cancel 为取消
[_searchController.searchBar setValue:@"取消" forKey:@"_cancelButtonText"];
//这两句设置了设置了似乎也没有什么用,筛选结果页会先下偏移44,
_searchController.definesPresentationContext = YES;
_searchController.hidesNavigationBarDuringPresentation = YES;
//above
//是否设置半透明覆盖层
_searchController.dimsBackgroundDuringPresentation = YES;
_searchController.searchBar.delegate = self;
[_searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = _searchController.searchBar;
}
2.2 设置筛选数据的显示
#pragma mark - UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString * searchString = searchController.searchBar.text;
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",searchString];
self.filteredArray = [[self.dataArray filteredArrayUsingPredicate:predicate] mutableCopy];
_resultVC.dataArray = self.filteredArray;
[_resultVC.tableView reloadData];
}
效果图: