#import "ViewController.h"
@interface ViewController () <UISearchBarDelegate, UISearchResultsUpdating>
@property (strong, nonatomic) UISearchController *searchController;
//全部數據
@property (nonatomic, strong) NSArray *listTeams;
//過濾后的數據
@property (nonatomic, strong) NSMutableArray *listFilterTeams;
//內容過濾方法
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSUInteger)scope;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"team"
ofType:@"plist"];
//獲取屬性列表文件中的全部數據
self.listTeams = [[NSArray alloc] initWithContentsOfFile:plistPath];
//查詢所有數據
[self filterContentForSearchText:@"" scope:-1];
//實例化UISearchController
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
//設置self為更新搜索結果對象
self.searchController.searchResultsUpdater = self;
//在搜索是背景設置為灰色
self.searchController.dimsBackgroundDuringPresentation = FALSE;
//設置搜索范圍欄中的按鈕
self.searchController.searchBar.scopeButtonTitles = @[@"名字", @"圖片"];
self.searchController.searchBar.delegate = self;
//將搜索欄放到表視圖的表頭中
self.tableView.tableHeaderView = self.searchController.searchBar;
[self.searchController.searchBar sizeToFit];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark --內容過濾方法
- (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://中文 name字段是中文名
//SELF.name contains[c] %@是Predicate字符串,SELF.name是要查詢的對象的name字段,contains[c]是包含字符的意思
scopePredicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];
tempArray =[self.listTeams filteredArrayUsingPredicate:scopePredicate];
self.listFilterTeams = [NSMutableArray arrayWithArray:tempArray];
break;
case 1: //英文 image字段保存英文名
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 {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier" forIndexPath:indexPath];
NSUInteger row = [indexPath row];
NSDictionary *rowDict = self.listFilterTeams[row];
cell.textLabel.text = rowDict[@"name"];
cell.detailTextLabel.text = rowDict[@"image"];
NSString *imagePath = [[NSString alloc] initWithFormat: @"%@.png", rowDict[@"image"]];
cell.imageView.image = [UIImage imageNamed:imagePath];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
//當切換"名字"和"圖片"按鈕時調用
#pragma mark --實現UISearchBarDelegate協議方法
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
[self updateSearchResultsForSearchController:self.searchController];
}
#pragma mark --實現UISearchResultsUpdating協議方法
//當搜索欄變成第一響應者,並且內容被改變時調用
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString *searchString = searchController.searchBar.text;
//查詢
[self filterContentForSearchText:searchString scope:searchController.searchBar.selectedScopeButtonIndex];
[self.tableView reloadData];
}
@end
屏幕截圖如下:

圖片.png