曾几何时,使用过UISearchBar,当时看到UISearchDisplayController简直是一头雾水,不知道怎么使用。最近的一个app中需要使用该功能,于是写了个Demo探究下。现在做下笔记。
首先往故事板中视图控制器中加入一个Search Bar and Search Display组件:
然后连接Outlets,包括搜索栏和SearchDisplayController:
在ViewController中声明它要遵守以下委托:
@interface ViewController () <UISearchBarDelegate,UISearchDisplayDelegate,UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) NSMutableArray *storeResults; @property (strong, nonatomic) NSMutableArray *results;
- (void)viewDidLoad { [super viewDidLoad]; _srcTableView.dataSource = self; _srcTableView.delegate = self; _searchBar.delegate = self; _mySearchDisplayController.searchResultsDataSource = self; _mySearchDisplayController.searchResultsDelegate = self; _mySearchDisplayController.delegate = self; self.storeResults = [NSMutableArray arrayWithObjects:@"广州", @"北京", @"上海", @"深圳", @"香港", @"广东", @"北师大", @"北大", @"香江", @"香菜", @"北海道", @"惠州", @"东莞", @"杭州", nil]; self.results = [NSMutableArray array]; }
#pragma mark - UISearchDisplayDelegate - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { [self filterContentForSearchText:searchString scope:[_searchBar scopeButtonTitles][_searchBar.selectedScopeButtonIndex]]; return YES; } -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption { [self filterContentForSearchText:_searchBar.text scope:_searchBar.scopeButtonTitles[searchOption]]; return YES; } -(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { NSMutableArray *tempResults = [NSMutableArray array]; NSUInteger searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch; for (int i = 0; i < _storeResults.count; i++) { NSString *storeString = _storeResults[i]; NSRange storeRange = NSMakeRange(0, storeString.length); NSRange foundRange = [storeString rangeOfString:searchText options:searchOptions range:storeRange]; if (foundRange.length) { [tempResults addObject:storeString]; } } [self.results removeAllObjects]; [self.results addObjectsFromArray:tempResults]; }
#pragma mark - UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView == _mySearchDisplayController.searchResultsTableView) { return _results.count; } else { return _storeResults.count; } } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *kCellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier]; } if (tableView == _mySearchDisplayController.searchResultsTableView) { cell.textLabel.text = _results[indexPath.row]; } else { cell.textLabel.text = _storeResults[indexPath.row]; } return cell; } #pragma mark - UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView == _mySearchDisplayController.searchResultsTableView) { NSLog(@"%@", _results[indexPath.row]); } else { NSLog(@"%@", _storeResults[indexPath.row]); } }为了区分正在展示的表格内容是搜索结果还是源数据,先要进行tableView ==
_mySearchDisplayController.searchResultsTableView的判断。
运行结果如下: