iOS原生的搜索:UISearchController
jopen
9年前
iOS8之前我们使用UISearchDisplayController做TableView的本地搜索,查看UIKit库,苹果已经使用新控件取代它。
NS_CLASS_DEPRECATED_IOS(3_0, 8_0, "UISearchDisplayController has been replaced with UISearchController") __TVOS_PROHIBITED
使用UISearchDisplayController的时候,搜索结果的展示tableView系统已经帮我们封装好,但是使用UISearchController,我们需要提供一个搜索结果的展示TableView.
如何使用UISearchController实现搜索功能呢?
1创建一个搜索结果展示TableViewController
.h文件
#import <UIKit/UIKit.h> @interface SearchResultVC : UITableViewController // 搜索结果数据 @property (nonatomic, strong) NSMutableArray *resultsArray; @end
.m
#import "SearchResultVC.h" @interface SearchResultVC () @end @implementation SearchResultVC - (void)viewDidLoad { [super viewDidLoad]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.resultsArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RESULT_CELL"]; if (cell == nil) { cell = [tableView dequeueReusableCellWithIdentifier:@"RESULT_CELL"]; } cell.textLabel.text = self.resultsArray[indexPath.row]; cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; return cell; }
2 创建一个UISearchController
- (void)initSearchController{ SearchResultVC *resultTVC = [[SearchResultVC alloc] initWithStyle:UITableViewStylePlain]; UINavigationController *resultVC = [[UINavigationController alloc] initWithRootViewController:resultTVC]; self.searchController = [[UISearchController alloc]initWithSearchResultsController:resultVC]; self.searchController.searchResultsUpdater = self; //self.searchController.dimsBackgroundDuringPresentation = NO; //self.searchController.hidesNavigationBarDuringPresentation = NO; self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x,self.searchController.searchBar.frame.origin.y,self.searchController.searchBar.frame.size.width,44); self.tableView.tableHeaderView = self.searchController.searchBar; self.searchController.searchBar.delegate = self; }
3 实现UISearchController的UISearchResultsUpdating方法,当开始搜索的时候响应。并且实现筛选的逻辑
#pragma mark - UISearchResultsUpdating - (void)updateSearchResultsForSearchController:(UISearchController *)searchController{ UINavigationController *navController = (UINavigationController *)self.searchController.searchResultsController; SearchResultVC *resultVC = (SearchResultVC *)navController.topViewController; [self filterContentForSearchText:self.searchController.searchBar.text]; resultVC.resultsArray = self.tempsArray; [resultVC.tableView reloadData]; } #pragma mark - Private Method - (void)filterContentForSearchText:(NSString *)searchText{ NSLog(@"%@",searchText); NSUInteger searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch; [self.tempsArray removeAllObjects]; for (int i = 0; i < self.resultArray.count; i++) { NSString *title = self.resultArray[i]; NSRange storeRange = NSMakeRange(0, title.length); NSRange foundRange = [title rangeOfString:searchText options:searchOptions range:storeRange]; if (foundRange.length) { [self.tempsArray addObject:self.resultArray[i]]; } } }
完成了,就是这么简单,UI什么的可以自定义。简单demo可以去我github上下载: https://github.com/wangdachui/WTUISearchController