UISearchController是iOS8.0后开始使用的类,它将UIsearchBar和显示结果的SearchResultsController封装在一起,非常方便。它的使用也很简单,代码如下:
- (void) searchControllerSubViews
{
self.searchController=[[UISearchController alloc]initWithSearchResultsController:[[UIViewController alloc]init]];
self.searchController.searchBar.frame=CGRectMake(100, 10, 200, 44);
self.brandDisplayTableView.tableHeaderView=self.searchController.searchBar;
[self.searchController.searchBar sizeToFit];
self.searchController.searchResultsUpdater=self;
//非常重要的一个属性,用来判断searchController是否在编辑状态
self.searchController.active = NO;
self.searchController.delegate = self;
self.searchController.searchBar.delegate = self;
self.searchController.dimsBackgroundDuringPresentation=YES;
self.searchController.hidesNavigationBarDuringPresentation=NO;
self.searchController.searchBar.placeholder=@"请输入品牌";
[self presentViewController:self.searchController animated:YES completion:nil];
}
//searchBar中每次内容改变,都会执行该方法
- (void) updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchString = [NSString stringWithUTF8String:self.searchController.searchBar.text.UTF8String];
NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];//用predicateWithFormat创建一个谓词,name作为键路径
if (self.searchResult!= nil) {
[self.searchResult removeAllObjects];
}
//过滤数据
self.searchResult= [NSMutableArray arrayWithArray:[self.searchList filteredArrayUsingPredicate:preicate]];
for (int i = 0;i < self.searchResult.count; i++) {
NSLog(@"%@" , self.searchResult[i]);
}
[self.brandDisplayTableView reloadData];
}
如果要实现简单的实时搜索功能,使用searchBar一样可以实现,而且我自己感觉使用起来要比searchController灵活一些
//searchBar的代理方法,searchBar每次改变内容,都会执行该方法
- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSString *searchString = [NSString stringWithUTF8String:self.searchBar.text.UTF8String];
NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];//用predicateWithFormat创建一个谓词,name作为键路径
if (self.searchResult!= nil) {
[self.searchResult removeAllObjects];
}
//过滤数据
self.searchResult= [NSMutableArray arrayWithArray:[self.searchList filteredArrayUsingPredicate:preicate]];
for (int i = 0;i < self.searchResult.count; i++) {
NSLog(@"%@" , self.searchResult[i]);
}
[self.brandDisplayTableView reloadData];
}
如果想要将搜索结果和页面内容共用一个tableView(搜索时显示结果,不搜索时显示页面内容),需要判断何时显示什么内容,我是使用数组是否为空,来进行判断
#pragma mark - 设置tableVIew
- (void) tableViewSubVIews
{
self.brandDisplayTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 108*Height, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64*Height - self.tabBarController.tabBar.frame.size.height - 44*Height) style:UITableViewStylePlain];
self.brandDisplayTableView.separatorStyle = 0;
self.brandDisplayTableView.delegate = self;
self.brandDisplayTableView.dataSource = self;
self.brandDisplayTableView.userInteractionEnabled = YES;
[self.view addSubview:self.brandDisplayTableView];
}
#pragma mark - 分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (self.searchResult.count!=0) {
return 1;
}
else{
return self.contentDic.allKeys.count;
}
}
#pragma mark - footer高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 1;
}
#pragma mark - header高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40*Height;
}
#pragma mark - 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80*Height;
}
#pragma mark - 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.searchResult.count!=0) {
return self.searchResult.count;
}
else{
NSString *letter = self.letterArr[section];
self.contentArr = [self.contentDic objectForKey:letter];
return self.contentArr.count;
}
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
if (self.searchResult.count!=0) {
return NULL;
}else{
return self.letterArr;
}
}
#pragma mark - 自定义header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (self.searchResult.count!=0)
{
BrandDisplayHeaderView *header = [[BrandDisplayHeaderView alloc]initWithReuseIdentifier:@"BrandDisplayHeader"];
return header;
}
else{
BrandDisplayHeaderView *header = [[BrandDisplayHeaderView alloc]initWithReuseIdentifier:@"BrandDisplayHeader"];
header.titleLabel.text = self.letterArr[section];
return header;
}
}
#pragma mark - tableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BrandDisplayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"brandDisplay"];
if (cell == nil) {
cell = [[BrandDisplayTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"brandDisplay"];
}
BrandDisplayModel *model = [[BrandDisplayModel alloc]init];
/**
* 搜索时
*/
if (self.searchResult.count!=0) {
NSMutableArray *arr = [self.contentDic objectsForKeys:self.letterArr notFoundMarker:@"0"].mutableCopy;
NSMutableArray *contentDic = [NSMutableArray array];
for (int i = 0; i < arr.count; i++) {
NSArray *arr1 = arr[i];
for (int j = 0; j < arr1.count; j++) {
NSDictionary *dic = arr1[j];
for (int m = 0; m < self.searchResult.count; m++) {
NSString *str = self.searchResult[m];
if ([[dic objectForKey:@"name"] isEqualToString:str]) {
NSLog(@"%@" , str);
[contentDic addObject:dic];
}
}
}
}
NSDictionary *dic = contentDic[indexPath.row];
[model setValuesForKeysWithDictionary:dic];
cell.model = model;
NSLog(@"brandid = %@" , model.name);
//cell.brandLabel.textColor = [UIColor redColor];
return cell;
}
/**
* 不搜索时
*/
else{
self.contentArr = [self.contentDic objectForKey:self.letterArr[indexPath.section]];
NSDictionary *dic = self.contentArr[indexPath.row];
[model setValuesForKeysWithDictionary:dic];
cell.model = model;
return cell;
}
}
这样就可以实现实时搜索了