搜索欄的重要性我們就不說了,狼廠就是靠搜索起家的,現(xiàn)在越來越像一匹沒有節(jié)操的狼,UC瀏覽器搜索欄現(xiàn)在默認(rèn)自家的神馬搜索,現(xiàn)在不管是社交,O2O還是在線教育等都會有一個搜索欄的實(shí)現(xiàn),不過彼此實(shí)現(xiàn)效果是不一樣的。iOS中的搜索欄實(shí)現(xiàn)起來相對簡單一點(diǎn),網(wǎng)上也有很多參考資料,不過靠譜的不是很多,很多都是iOS 8.0之前的實(shí)現(xiàn),iOS 8.0上的實(shí)現(xiàn)貌似很少看到,看了一些老外的代碼,使用了一下UISearchController感覺還是非常不錯的。 UISearchBar和UIDisplayController是網(wǎng)上最常見的也算是最簡單的,也有使用Searh Bar Search Display Controller的控件的,本文就簡單的使用Search Bar和UITableView實(shí)現(xiàn)搜索Demo的,最上面的就是搜索欄,之前的就是TableView: 為了實(shí)現(xiàn)搜索需要聲明委托 UISearchBarDelegate , UISearchDisplayDelegate,其中搜索主要使用的就是UISearchDisplayDelegate,具體代碼實(shí)現(xiàn)過程: 聲明字段: @property (strong,nonatomic) NSMutableArray *dataList; @property (strong,nonatomic) NSMutableArray *searchList; 初始化數(shù)據(jù): self.dataList=[NSMutableArray arrayWithCapacity:100]; for (NSInteger i=0; i<100; i++) { [self.dataList addObject:[NSString stringWithFormat:@"%ld-FlyElephant",(long)i]]; } 設(shè)置區(qū)域: //設(shè)置區(qū)域 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } 設(shè)置區(qū)域的行數(shù)(重點(diǎn)),這個就是使用委托之后需要需要判斷是一下是否是需要使用Search之后的視圖: -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (tableView == self.searchDisplayController.searchResultsTableView) { return [self.searchList count]; }else{ return [self.dataList count]; } } 同樣的返回單元格也有兩種情況,一種是初始化數(shù)據(jù),一種是過濾之后的數(shù)據(jù)視圖: -(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中德 開始和結(jié)束的事件: - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{ NSLog(@"搜索Begin"); return YES; } - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{ NSLog(@"搜索End"); return YES; } 搜索時過濾數(shù)據(jù): - (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]; } //過濾數(shù)據(jù) self.searchList= [NSMutableArray arrayWithArray:[_dataList filteredArrayUsingPredicate:preicate]]; //刷新表格 return YES; } 最終效果如下: UISearchController實(shí)現(xiàn)搜索UISeachBar通過UISearchDisplayDelegate實(shí)現(xiàn)上面的效果是沒有問題的,網(wǎng)上也有很多類似的實(shí)現(xiàn)效果,不過是警告的,信息如下: 'searchDisplayController' is deprecated: first deprecated in iOS 8.0,這么明顯一個警告總不能視而不見吧 , 在 StackOverFlow 中發(fā)現(xiàn) UISearchDisplayController is deprecated in IOS8 .0 , and recommended to use UISearchController instead ,也就是說 iOS 8.0 不推薦 UISearchDisplayController, 也就是不推薦使用 UISearchDisplayDelegate ,但是可以通過 UISearchController 實(shí)現(xiàn) UISearchResultsUpdating 這個委托實(shí)現(xiàn)上面的效果; 視圖中中需要聲明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,不過現(xiàn)在直接使用self.searchController.active進(jìn)行判斷即可,也就是UISearchController的active屬性: //設(shè)置區(qū)域的行數(shù) -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (self.searchController.active) { return [self.searchList count]; }else{ return [self.dataList count]; } } //返回單元格內(nèi)容 -(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; } 具體調(diào)用的時候使用的方法也發(fā)生了改變,這個時候使用updateSearchResultsForSearchController進(jìn)行結(jié)果過濾: -(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]; } //過濾數(shù)據(jù) self.searchList= [NSMutableArray arrayWithArray:[_dataList filteredArrayUsingPredicate:preicate]]; //刷新表格 [self.tableView reloadData]; } 效果演示: 不過兩者最終實(shí)現(xiàn)的效果的效果基本上是一致,殊途同歸,本文難免有所遺漏,如有不當(dāng),請多多指正~ 參考資料: |
|