iOS中UISearchDisplayController用於搜索,搜索欄的重要性我們就不說了,狼廠就是靠搜索起家的,現在越來越像一匹沒有節操的狼,UC瀏覽器搜索欄現在默認自家的神馬搜索,現在不管是社交,O2O還是在線教育等都會有一個搜索欄的實現,不過彼此實現效果是不一樣的。iOS中的搜索欄實現起來相對簡單一點,網上也有很多參考資料,不過靠譜的不是很多,很多都是iOS 8.0之前的實現,iOS 8.0上的實現貌似很少看到,可以運行,不過會看到searchDisplayController' is deprecated: first deprecated in iOS 8.0警告,看了一些老外的代碼,使用了一下UISearchController感覺還是非常不錯的。
UISearchBar和UISearchDisplayController
是網上最常見的也算是最簡單的,也有使用Searh Bar Search Display Controller的控件的,本文就簡單的使用Search Bar和UITableView實現搜索Demo的,最上面的就是搜索欄,之前的就是TableView:
為了實現搜索需要聲明委托UISearchBarDelegate,UISearchDisplayDelegate,其中搜索主要使用的就是UISearchDisplayDelegate,具體代碼實現過程:
聲明字段:
@property (strong,nonatomic) NSMutableArray *dataList; @property (strong,nonatomic) NSMutableArray *searchList;
初始化數據:
self.dataList=[NSMutableArray arrayWithCapacity:100]; for (NSInteger i=0; i<100; i++) { [self.dataList addObject:[NSString stringWithFormat:@"%ld-FlyElephant",(long)i]]; }
設置區域:
//設置區域 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; }
設置區域的行數(重點),這個就是使用委托之后需要需要判斷是一下是否是需要使用Search之后的視圖:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (tableView == self.searchDisplayController.searchResultsTableView) { return [self.searchList count]; }else{ return [self.dataList count]; } }
同樣的返回單元格也有兩種情況,一種是初始化數據,一種是過濾之后的數據視圖:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *flag=@"cellFlag"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag]; if (cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag]; } if (tableView==self.searchDisplayController.searchResultsTableView) { [cell.textLabel setText:self.searchList[indexPath.row]]; } else{ [cell.textLabel setText:self.dataList[indexPath.row]]; } return cell; }
UISearchBarDelegate中的開始和結束的事件:
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{ NSLog(@"搜索Begin"); return YES; } - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{ NSLog(@"搜索End"); return YES; }
搜索時過濾數據:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{ // 謂詞的包含語法,之前文章介紹過http://www.cnblogs.com/xiaofeixiang/ NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString]; if (self.searchList!= nil) { [self.searchList removeAllObjects]; } //過濾數據 self.searchList= [NSMutableArray arrayWithArray:[_dataList filteredArrayUsingPredicate:preicate]]; //刷新表格 return YES; }
最終效果如下:
UISearchController實現搜索
UISeachBar通過UISearchDisplayDelegate實現上面的效果是沒有問題的,網上也有很多類似的實現效果,不過是警告的,信息如下: 'searchDisplayController' is deprecated: first deprecated in iOS 8.0,這么明顯一個警告總不能視而不見吧,在StackOverFlow中發現UISearchDisplayController is deprecated in IOS8.0, and recommended to use UISearchController instead,也就是說iOS 8.0不推薦UISearchDisplayController,也就是不推薦使用UISearchDisplayDelegate,但是可以通過UISearchController實現UISearchResultsUpdating這個委托實現上面的效果;
視圖中中需要聲明UISearchResultsUpdating:
@interface ViewController : UITableViewController<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,UISearchResultsUpdating> @end
屬性聲明:
@property (nonatomic, strong) UISearchController *searchController;
需要自己初始化一下UISearchController:
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; _searchController.searchResultsUpdater = self; _searchController.dimsBackgroundDuringPresentation = 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); self.tableView.tableHeaderView = self.searchController.searchBar;
之前是通過判斷搜索時候的TableView,不過現在直接使用self.searchController.active進行判斷即可,也就是UISearchController的active屬性:
//設置區域的行數 -(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=@"cellFlag"; 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; }
具體調用的時候使用的方法也發生了改變,這個時候使用updateSearchResultsForSearchController進行結果過濾:
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController { 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]; }
效果演示:
不過兩者最終實現的效果的效果基本上是一致,殊途同歸,本文難免有所遺漏,如有不當,請多多指正~
參考資料:
2016.05.10 補充
如果需要設置搜索的文字可以通過searchBar設置:
searchController.searchBar.placeholder=@"FlyElephant最新博客-http://www.jianshu.com/users/24da48b2ddb3/latest_articles";