// // FirstViewController.m // UISearchBarControllerText // // Created by 杜承玖 on 14/10/24. // Copyright (c) 2014年 com.redianying. All rights reserved. // #import "FirstViewController.h" @interface FirstViewController () <UISearchBarDelegate, UISearchDisplayDelegate, UITableViewDataSource, UITableViewDelegate> @property (nonatomic, strong) UISearchBar * sb; @property (nonatomic, strong) UITableView * tv; @property (nonatomic, strong) UISearchDisplayController * sdc; @property (nonatomic, copy) NSArray *allItems; @property (nonatomic, copy) NSArray *searchResults; @end @implementation FirstViewController - (void)viewDidLoad { [super viewDidLoad]; NSArray *items = @[@"Code Geass", @"Asura Cryin'", @"Voltes V", @"Mazinger Z", @"Daimos"]; self.allItems = items; _sb = [[UISearchBar alloc] init]; _sb.placeholder = @"输入名称"; _sb.barStyle = UIBarStyleDefault; _sb.delegate = self; [_sb setScopeButtonTitles:@[@"First", @"Last"]]; [_sb setAutocapitalizationType:UITextAutocapitalizationTypeNone]; _tv = [[UITableView alloc] initWithFrame:self.view.frame]; _tv.frame = CGRectMake(0, 22, 320, 500); _tv.delegate = self; _tv.dataSource = self; [self.view addSubview:_tv]; [_tv setTableHeaderView:_sb]; _sdc = [[UISearchDisplayController alloc] initWithSearchBar:_sb contentsController:self]; _sdc.delegate = self; [_sdc setSearchResultsDataSource:self]; [_sdc setSearchResultsDelegate:self]; } #pragma mark - SearchBar - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{ // _tv.frame = CGRectMake(0, 22, 320, 500); return YES; } - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{ // _tv.frame = CGRectMake(0, 22, 320, 500); return YES; } #pragma mark - SearchDisplayController - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{ [self filterContentForSearchText:searchString scope: [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; return YES; } - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption{ [self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope: [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]]; return YES; } #pragma mark - Function - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText]; self.searchResults = [self.allItems filteredArrayUsingPredicate:resultPredicate]; } #pragma mark - UITableView - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSInteger rows = 0; if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){ rows = [self.searchResults count]; }else{ rows = [self.allItems count]; } return rows; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } /* Configure the cell. */ if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){ cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row]; }else{ cell.textLabel.text = [self.allItems objectAtIndex:indexPath.row]; } return cell; } @end
UISearchBar和UISearchBarController的使用
猜你喜欢
转载自duchengjiu.iteye.com/blog/2147601
今日推荐
周排行