iOS中UISearchDisplayController用於搜索,搜索欄的重要性咱們就不說了,狼廠就是靠搜索起家的,如今愈來愈像一匹沒有節操的狼,UC瀏覽器搜索欄如今默認自家的神馬搜索,如今無論是社交,O2O仍是在線教育等都會有一個搜索欄的實現,不過彼此實現效果是不同的。iOS中的搜索欄實現起來相對簡單一點,網上也有不少參考資料,不過靠譜的不是不少,不少都是iOS 8.0以前的實現,iOS 8.0上的實現貌似不多看到,能夠運行,不過會看到searchDisplayController' is deprecated: first deprecated in iOS 8.0警告,看了一些老外的代碼,使用了一下UISearchController感受仍是很是不錯的。html
是網上最多見的也算是最簡單的,也有使用Searh Bar Search Display Controller的控件的,本文就簡單的使用Search Bar和UITableView實現搜索Demo的,最上面的就是搜索欄,以前的就是TableView:ios
爲了實現搜索須要聲明委託UISearchBarDelegate,UISearchDisplayDelegate,其中搜索主要使用的就是UISearchDisplayDelegate,具體代碼實現過程:瀏覽器
聲明字段:app
@property (strong,nonatomic) NSMutableArray *dataList; @property (strong,nonatomic) NSMutableArray *searchList;
初始化數據:ide
self.dataList=[NSMutableArray arrayWithCapacity:100]; for (NSInteger i=0; i<100; i++) { [self.dataList addObject:[NSString stringWithFormat:@"%ld-FlyElephant",(long)i]]; }
設置區域:atom
//設置區域 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; }
設置區域的行數(重點),這個就是使用委託以後須要須要判斷是一下是不是須要使用Search以後的視圖:spa
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (tableView == self.searchDisplayController.searchResultsTableView) { return [self.searchList count]; }else{ return [self.dataList count]; } }
一樣的返回單元格也有兩種狀況,一種是初始化數據,一種是過濾以後的數據視圖:orm
-(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中的開始和結束的事件:htm
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{ NSLog(@"搜索Begin"); return YES; } - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{ NSLog(@"搜索End"); return YES; }
搜索時過濾數據:blog
- (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; }
最終效果以下:
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";