//
// ViewController.m
// SearchbarTable
//
#import "ViewController.h"
@interface ViewController ()<UISearchBarDelegate,UISearchResultsUpdating>//实现两个协议
@property (strong,nonatomic) UISearchController *searchController;
//过滤后的数据
@property (strong,nonatomic) NSMutableArray *listFilterTeams;
//内容过滤方法
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSUInteger)scope;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
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.dimsBackgroundDuringPresentation = FALSE;
//设置搜索范围栏中的按钮
self.searchController.searchBar.scopeButtonTitles = @[@"中文",@"英文"];
self.searchController.searchBar.delegate = self;
//将搜索栏放在表视图的表头中
self.tableView.tableHeaderView = self.searchController.searchBar;
[self.searchController.searchBar sizeToFit];
}
-(void)filterContentForSearchText:(NSString *)searchText scope:(NSUInteger)scope{
if ([searchText length]==0) {
self.listTeams = [NSMutableArray arrayWithArray:self.listTeams];
return;
}
NSPredicate *scopePredicate;
NSArray *tempArray;
switch (scope) {
case 0:
scopePredicate = [NSPredicate predicateWithFormat:@"SELF.name contain[c] %@",searchText];
tempArray = [self.listTeams filteredArrayUsingPredicate:scopePredicate];
self.listFilterTeams = [NSMutableArray arrayWithArray:tempArray];
break;
case 1:
scopePredicate = [NSPredicate predicateWithFormat:@"SELF.image contain[c] %@",searchText];
tempArray = [self.listTeams filteredArrayUsingPredicate:scopePredicate];
self.listFilterTeams = [NSMutableArray arrayWithArray:tempArray];
break;
default:
self.listFilterTeams = [NSMutableArray arrayWithArray:self.listTeams];
break;
}
}
#pragma mark -- 实现UISearchBarDelegate协议方法
-(void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{
[self updateSearchResultsForSearchController:self.searchController];
}
#pragma mark -- 实现UISearchBarDelegate协议方法
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
NSString *searchString = searchController.searchBar.text;
[self filterContentForSearchText:searchString scope:searchController.searchBar.selectedScopeButtonIndex];
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//返回某个节点的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.listTeams count];
}
//为单元格提供数
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//只需要填写一个值@"CellIdentifier" ,这个值是之前自己设置的cell名称
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier" forIndexPath:indexPath];
NSUInteger row = [indexPath row];//得到了数组的索引
NSDictionary *rowDict = self.listTeams[row];//数组的索引就是字典
cell.textLabel.text = rowDict[@"name"];//单元格的主标题文字
cell.detailTextLabel.text = rowDict[@"image"];//单元格的辅标题文字
//cell.textLabel.text = rowDict[@"image"];
NSString *imagePath = [[NSString alloc] initWithFormat:@"%@.png",rowDict[@"image"]];//图片路径
cell.imageView.image = [UIImage imageNamed:imagePath];//设置图片
return cell;
}
@end
ios开发——添加搜索栏
最新推荐文章于 2018-09-24 10:57:21 发布