IOS UISearchController and iosuisearchbar
In iOS9, UISearchDisplayController has been replaced by UISearchController. The search box is a common control.
Assume that we want to meet the requirements, generate 100 "numbers + three random letters", and then search for Results containing a certain letter.
So what should we do?
# Import "ViewController. h "@ interface ViewController () <UITableViewDelegate, UITableViewDataSource, metrics, metrics> // tableView @ property (strong, nonatomic) UITableView * tableView; // searchController @ property (strong, nonatomic) UISearchController * searchController; // data source @ 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]; // generates 100 "numbers + three random letters" 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; // create UISearchController _ searchController = [[UISearchController alloc] initWithSearchResultsController: nil]; // set proxy _ searchController. delegate = self; _ searchContr Oller. searchResultsUpdater = self; // set the display attribute of UISearchController. The following three attributes are YES by default. // when searching, the background fades into the color _ searchController. dimsBackgroundDuringPresentation = NO; // when searching, the background changes to fuzzy _ searchController. obscuresBackgroundDuringPresentation = NO; // hide the navigation bar _ searchController. hidesNavigationBarDuringPresentation = NO; _ searchController. searchBar. frame = CGRectMake (self. searchController. searchBar. frame. origin. x, self. searchControll Er. searchBar. frame. origin. y, self. searchController. searchBar. frame. size. width, 44.0); // Add searchbar to headerview self. tableView. tableHeaderView = _ searchController. searchBar; [self. view addSubview: _ tableView]; // Do any additional setup after loading the view, typically from a nib .} // generate 3 random letters-(NSString *) shuffledAlphabet {NSMutableArray * shuffledAlphabet = [NSMutableArray arrayWithArray: @ [@" ", @" 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 ;} // set the number of rows in the region-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {if (self. searchController. active) {return [self. searchList count];} else {return [self. dataList count] ;}}// return cell content-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * flag = @ "cell "; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: flag]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithSty Le: 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 agent // test the execution process of UISearchController-(void) willPresentSearchController :( UISearchController *) searchController {NSLog (@ "willPresentSearchCo Ntroller ");}-(void) didPresentSearchController :( UISearchController *) searchController {NSLog (@" didPresentSearchController ");}-(void) willDismissSearchController :( UISearchController *) searchController {NSLog (@ "willDismissSearchController");}-(void) didDismissSearchController :( UISearchController *) searchController {NSLog (@ "didDismissSearchController");}-(void) presentSearchController :( listener *) 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];} // filter data self. searchList = [NSMutableArray arrayWithArray: [_ dataList filteredArrayUsingPredicate: preicate]; // refresh the table [self. tableView reloadData];}
The execution process of the UISearchController proxy method:
UISearchController usage steps:1. Create
// Create UISearchController _ searchController = [[UISearchController alloc] initWithSearchResultsController: nil];
2. Set proxy
// Set proxy _ searchController. delegate = self; _ searchController. searchResultsUpdater = self;
3. Set attributes
// Set the display attribute of UISearchController. The following three attributes are YES by default. // when searching, the background is dimmed _ searchController. dimsBackgroundDuringPresentation = NO; // when searching, the background changes to fuzzy _ searchController. obscuresBackgroundDuringPresentation = NO; // hide the navigation bar _ searchController. hidesNavigationBarDuringPresentation = NO;
4. Implement proxy
- (void)willPresentSearchController:(UISearchController *)searchController;- (void)didPresentSearchController:(UISearchController *)searchController;- (void)willDismissSearchController:(UISearchController *)searchController;- (void)didDismissSearchController:(UISearchController *)searchController;- (void)presentSearchController:(UISearchController *)searchController;- (void)updateSearchResultsForSearchController:(UISearchController *)searchController;
Note:
1. If you want to display search results in the same view, use [UISearchController alloc] initWithSearchResultsController: nil]. However, this operation does not support TVOS. You must specify the result controller to provide TVOS.
[[UISearchController alloc] initWithSearchResultsController: VC] to implement the specified result controller.