UITableView search: SearchBar and SearchDisplayController; UISearchController,
I have previously written an article about using predicate search in tableView. At that time, I used a custom textField. Recently I was writing an e-book and needed to implement the search function in the bibliography. so here we will share with you the SearchBar and SearchDisplayController methods previously used to implement the search function system. however, when iOS8 lags behind, Apple no longer recommends that we use these two old items. Instead, we use UISearchController. Naturally, I will demonstrate the usage of this new thing here.
SearchBar and SearchDisplayController
Add tableView to the xib View File, and add SearchBar and SearchDisplayController to the table .:
The required Protocols are: <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate>
I declare two pieces of data here to save search data and search results:
@ Property (nonatomic, retain) NSMutableArray * dataArray;
@ Property (nonatomic, retain) NSMutableArray * resultArray;
Initialize the array:
Self. dataArray = [[NSMutableArray alloc] initWithObjects: @ "Zhang San", @ "Li Si", @ "Wang Wu", @ "a Wu", @ "Yun Fei", @ "asd ", nil];
In fact, there are two tables, one is all the data before the search, and the other is to display the search results. Therefore, we need to differentiate the tabbleView protocol:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (tableView==self.tableView) { return self.dataArray.count; } else { return self.resultArray.count; }}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *str=@"cell"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str]; if (cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str]; } if (tableView==self.tableView) { cell.textLabel.text=self.dataArray[indexPath.row]; } else { cell.textLabel.text=self.resultArray[indexPath.row]; } return cell;}
Next is the protocol method of UISearchBarDelegate and UISearchDisplayDelegate.
-(BOOL) searchBarShouldBeginEditing :( UISearchBar *) searchBar {NSLog (@ "Search Start"); return YES;} (BOOL) searchBarShouldEndEditing :( UISearchBar *) searchBar {NSLog (@ "search ended"); return YES;}-(BOOL) searchDisplayController :( UISearchDisplayController *) controller restart :( NSString *) searchString {NSPredicate * preicate = [NSPredicate predicateWithFormat: @ "self contains [c] % @", searchString]; if (Self. resultArray! = Nil) {[self. resultArray removeAllObjects];} self. resultArray = [NSMutableArray arrayWithArray: [self. dataArray filteredArrayUsingPredicate: preicate]; NSLog (@ "% lu", (unsigned long) self. resultArray. count); return YES ;}
The key code or predicate we use to distinguish between them. Compared with regular expressions, the predicate is very simple, but I still can't remember it.
The running result is as follows:
Although this is very useful, Apple has already mentioned that it has been highlighted in ios8. we recommend that you use UISearchController.
The following protocols must be added: <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchResultsUpdating>
Still drag a tabView to the xib file. To avoid mutual influence, I put the two in different views.
The two arrays are also declared, with the same functions as above. There is also a UIsearchController (because I don't know which control to drag, I have to write it manually ):
@ Property (nonatomic, retain) UISearchController * seachController;
@ Property (nonatomic, retain) NSMutableArray * resultArray;
@ Property (nonatomic, retain) NSMutableArray * dataArray;
Initialize the array and manually create searchController:
Self. dataArray = [[NSMutableArray alloc] initWithObjects: @ "Zhang San", @ "Li Si", @ "Wang Wu", @ "a Wu", @ "Yun Fei", @ "asd ", nil]; self. seachController = [[UISearchController alloc] initWithSearchResultsController: nil]; self. seachController. searchResultsUpdater = self; self. seachController. dimsBackgroundDuringPresentation = NO; self. seachController. hidesNavigationBarDuringPresentation = NO; self. seachController. searchBar. frame = CGRectMake (self. seachController. searchBar. frame. origin. x, self. seachController. searchBar. frame. origin. y, self. seachController. searchBar. frame. size. width, 44.0); self. tableView. tableHeaderView = self. seachController. searchBar;
In the same way, there are two search results, so you need to judge:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (!self.seachController.active) { return self.dataArray.count; } else { return self.resultArray.count; }}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *str=@"cell"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str]; if (cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str]; } if (!self.seachController.active) { cell.textLabel.text=self.dataArray[indexPath.row]; } else { cell.textLabel.text=self.resultArray[indexPath.row]; } return cell;}
Protocol method. Here we use a predicate to judge
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{ NSString *searchString=self.seachController.searchBar.text; NSPredicate *preicate=[NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@",searchString]; if (self.resultArray!=nil) { [self.resultArray removeAllObjects]; } self.resultArray=[NSMutableArray arrayWithArray:[self.dataArray filteredArrayUsingPredicate:preicate]]; NSLog(@"%lu",(unsigned long)self.resultArray.count); [self.tableView reloadData];}
Result demonstration:
Most of the essays I wrote have source code. If you want them, contact me. My personal information contains my QQ.