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