在iOS9中,UISearchDisplayController 已經被UISearchController替代。搜索框是一種常用的控件。
假設我們要滿足下圖的需求,產生100個“數字+三個隨機字母”,然后搜索包含某個字母的結果。
那么,該怎么做呢?

#import "ViewController.h" @interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchControllerDelegate,UISearchResultsUpdating> //tableView @property (strong, nonatomic) UITableView *tableView; //searchController @property (strong, nonatomic) UISearchController *searchController; //數據源 @property (strong,nonatomic) NSMutableArray *dataList; @property (strong,nonatomic) NSMutableArray *searchList; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _dataList = [NSMutableArray array]; _searchList = [NSMutableArray array]; self.dataList=[NSMutableArray arrayWithCapacity:100]; //產生100個“數字+三個隨機字母” for (NSInteger i=0; i<100; i++) { [self.dataList addObject:[NSString stringWithFormat:@"%ld%@",(long)i,[self shuffledAlphabet]]]; } _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20,[UIScreen mainScreen].bounds.size.width ,[UIScreen mainScreen].bounds.size.height)]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.separatorStyle = UITableViewCellSelectionStyleNone; //創建UISearchController _searchController = [[UISearchController alloc]initWithSearchResultsController:nil]; //設置代理 _searchController.delegate = self; _searchController.searchResultsUpdater= self; //設置UISearchController的顯示屬性,以下3個屬性默認為YES //搜索時,背景變暗色 _searchController.dimsBackgroundDuringPresentation = NO; //搜索時,背景變模糊 _searchController.obscuresBackgroundDuringPresentation = NO; //隱藏導航欄 _searchController.hidesNavigationBarDuringPresentation = NO; _searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0); // 添加 searchbar 到 headerview self.tableView.tableHeaderView = _searchController.searchBar; [self.view addSubview:_tableView]; // Do any additional setup after loading the view, typically from a nib. } //產生3個隨機字母 - (NSString *)shuffledAlphabet { NSMutableArray * shuffledAlphabet = [NSMutableArray arrayWithArray:@[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"]]; NSString *strTest = [[NSString alloc]init]; for (int i=0; i<3; i++) { int x = arc4random() % 25; strTest = [NSString stringWithFormat:@"%@%@",strTest,shuffledAlphabet[x]]; } return strTest; } //設置區域的行數 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (self.searchController.active) { return [self.searchList count]; }else{ return [self.dataList count]; } } //返回單元格內容 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *flag=@"cell"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag]; if (cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag]; } if (self.searchController.active) { [cell.textLabel setText:self.searchList[indexPath.row]]; } else{ [cell.textLabel setText:self.dataList[indexPath.row]]; } return cell; } #pragma mark - UISearchControllerDelegate代理 //測試UISearchController的執行過程 - (void)willPresentSearchController:(UISearchController *)searchController { NSLog(@"willPresentSearchController"); } - (void)didPresentSearchController:(UISearchController *)searchController { NSLog(@"didPresentSearchController"); } - (void)willDismissSearchController:(UISearchController *)searchController { NSLog(@"willDismissSearchController"); } - (void)didDismissSearchController:(UISearchController *)searchController { NSLog(@"didDismissSearchController"); } - (void)presentSearchController:(UISearchController *)searchController { NSLog(@"presentSearchController"); } -(void)updateSearchResultsForSearchController:(UISearchController *)searchController { NSLog(@"updateSearchResultsForSearchController"); NSString *searchString = [self.searchController.searchBar text]; NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString]; if (self.searchList!= nil) { [self.searchList removeAllObjects]; } //過濾數據 self.searchList= [NSMutableArray arrayWithArray:[_dataList filteredArrayUsingPredicate:preicate]]; //刷新表格 [self.tableView reloadData]; }
UISearchController代理方法的執行過程:

UISearchController的使用步驟:
1、創建
//創建UISearchController _searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
2、設置代理
//設置代理 _searchController.delegate = self; _searchController.searchResultsUpdater= self;
3、設置屬性
//設置UISearchController的顯示屬性,以下3個屬性默認為YES //搜索時,背景變暗色 _searchController.dimsBackgroundDuringPresentation = NO; //搜索時,背景變模糊 _searchController.obscuresBackgroundDuringPresentation = NO; //隱藏導航欄 _searchController.hidesNavigationBarDuringPresentation = NO;
4、實現代理
- (void)willPresentSearchController:(UISearchController *)searchController; - (void)didPresentSearchController:(UISearchController *)searchController; - (void)willDismissSearchController:(UISearchController *)searchController; - (void)didDismissSearchController:(UISearchController *)searchController; - (void)presentSearchController:(UISearchController *)searchController; - (void)updateSearchResultsForSearchController:(UISearchController *)searchController;
注意點:
1、如果你希望在同一個視圖中顯示搜索結果,則通過[[UISearchController alloc]initWithSearchResultsController:nil]。但是這是不支持TVOS,請提供TVOS一定要指定結果控制器。
[[UISearchController alloc]initWithSearchResultsController:VC],可以實現指定結果控制器。
