1、案例介绍:一个具备搜索功能的表视图,如图01,02,03
图01图02图03
2、Main.storyboard,如图04
图04
3、.h
#import <UIKit/UIKit.h> @interface CQ23ViewController : UITableViewController<UISearchBarDelegate, UISearchDisplayDelegate> // 搜索栏 @property (weak, nonatomic) IBOutlet UISearchBar *searchBar; // 所有队伍集合 @property (nonatomic, strong) NSArray *listTeams; // 查询后的队伍集合 @property (nonatomic, strong) NSMutableArray *listFilterTeams; // 按条件查询,加载查询出的内容,给listFilterTeams赋值 - (void)filterContentForSearchText:(NSString*)searchText scope:(NSUInteger)scope; @end
4、.m
#import "CQ23ViewController.h" @interface CQ23ViewController () @end @implementation CQ23ViewController - (void)viewDidLoad { [super viewDidLoad]; //设定搜索栏ScopeBar隐藏 [self.searchBar setShowsScopeBar:NO]; [self.searchBar sizeToFit]; NSBundle *bundle = [NSBundle mainBundle]; NSString *plistPath = [bundle pathForResource:@"team" ofType:@"plist"]; //获取属性列表文件中的全部数据 self.listTeams = [[NSArray alloc] initWithContentsOfFile:plistPath]; //初次进入查询所有数据 [self filterContentForSearchText:@"" scope:-1]; } - (void)viewDidUnload { [self setSearchBar:nil]; [super viewDidUnload]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } #pragma mark Content Filtering - (void)filterContentForSearchText:(NSString*)searchText scope:(NSUInteger)scope; { if([searchText length]==0) { //查询所有 self.listFilterTeams = [NSMutableArray arrayWithArray:self.listTeams]; return; } NSPredicate *scopePredicate; NSArray *tempArray ; switch (scope) { case 0: //英文 scopePredicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText]; tempArray =[self.listTeams filteredArrayUsingPredicate:scopePredicate]; self.listFilterTeams = [NSMutableArray arrayWithArray:tempArray]; break; case 1: scopePredicate = [NSPredicate predicateWithFormat:@"SELF.image contains[c] %@",searchText]; tempArray =[self.listTeams filteredArrayUsingPredicate:scopePredicate]; self.listFilterTeams = [NSMutableArray arrayWithArray:tempArray]; break; default: //查询所有 self.listFilterTeams = [NSMutableArray arrayWithArray:self.listTeams]; break; } } #pragma mark --UITableViewDataSource 协议方法 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.listFilterTeams count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } NSUInteger row = [indexPath row]; NSDictionary *rowDict = [self.listFilterTeams objectAtIndex:row]; cell.textLabel.text = [rowDict objectForKey:@"name"]; cell.detailTextLabel.text = [rowDict objectForKey:@"image"]; NSString *imagePath = [rowDict objectForKey:@"image"]; imagePath = [imagePath stringByAppendingString:@".png"]; cell.imageView.image = [UIImage imageNamed:imagePath]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; } #pragma mark --UISearchBarDelegate 协议方法 - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { // 当点击取消按钮,查询所有 [self filterContentForSearchText:@"" scope:-1]; } #pragma mark - UISearchDisplayController Delegate Methods - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { // 当文本内容发生改变时候,向表视图数据源发出重新加载消息 [self filterContentForSearchText:searchString scope:self.searchBar.selectedScopeButtonIndex]; //YES情况下表视图可以重新加载 return YES; } - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption { // 当Scope Bar选择发生变化时候,向表视图数据源发出重新加载消息 [self filterContentForSearchText:self.searchBar.text scope:searchOption]; // YES情况下表视图可以重新加载 return YES; } @end