美文网首页
ios搜索栏之UISearchController

ios搜索栏之UISearchController

作者: cj2527 | 来源:发表于2016-06-17 20:19 被阅读1342次

搜索栏的应用场景

场景一:有列表数据的搜索,例如通讯录,有一个联系人列表,用户点击顶部的搜索栏,当前联系人列表刷新数据,只返回要查找的数据.(网上例子很多)
场景二:没有列表数据的搜索,例如一个主界面,顶部有搜索栏,搜索栏下面有很多子控件,用户点击搜索,在显示结果列表选择其一,直接返回数据,并对子控件设置值。
本文讲的就是场景二的使用,置于场景一的使用,网上大把。

本文知识点:
UISearchController+UISearchBar+静态Cell+动态Cell+传值

新建工程

新建CJTableViewController类,
删除默认的VC,拖进UITableViewController控件,设置为启动。

9170FC35-3D69-49B1-BA25-B9AC70D36BD4.png

设置cell样式为静态Cell

BADAD61B-626C-410B-B666-8E02310F4D70.png

关联CJTableViewController类。

新建SearchResultViewController类,继承于UITableViewController
SearchResultViewController.h文件

#import <UIKit/UIKit.h>
@interface SearchResultViewController : UITableViewController
@property (nonatomic, strong) NSArray *searchDataArray;//查询的数据
@property (nonatomic, copy) NSString *uid;//定义一个属性,用来传值
@end

SearchResultViewController.m文件

#import "SearchResultViewController.h"

@interface SearchResultViewController ()

@end

@implementation SearchResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

//返回行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.searchDataArray count];
}
//返回cell内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *flag=@"cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag];
    }
    
    [cell.textLabel setText:self.searchDataArray[indexPath.row]];
    return cell;
}
//选中事件
-(void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    NSLog(@"%ld",(long)indexPath.row);
    //给属性设置值,用来传值
    self.uid =[NSString stringWithFormat:@"%d",indexPath.row];
}

在CJTableViewController.m文件
定义属性:
遵守协议:
实现代理方法:
删除默认的Table view data source的方法,完整代码如下:

#import "CJTableViewController.h"
#import "SearchResultViewController.h"

@interface CJTableViewController ()<UISearchResultsUpdating,UISearchBarDelegate>
@property (nonatomic, strong) UISearchController *searchController;
@property (strong,nonatomic) NSMutableArray  *contactsList;//全部联系人列表
@property (strong,nonatomic) NSMutableArray  *searchList;//查找的列表
@property (strong,nonatomic) SearchResultViewController *resultVC;//查询结果视图控制器
@end

@implementation CJTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //查找结果视图控制器
     self.resultVC = [[SearchResultViewController alloc] init];
    //设置searchController
    [self setup];
    //初始化数据
    [self loadData];
}

-(void)setup{

    //搜索之后的结果就显示在当前这个控制器上
     _searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultVC];
    
    //搜索时,背景变暗色
    _searchController.dimsBackgroundDuringPresentation = NO;
    //隐藏导航栏
    _searchController.hidesNavigationBarDuringPresentation = NO;
    
    _searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
    self.tableView.tableHeaderView = self.searchController.searchBar;
    
    _searchController.searchResultsUpdater = self;
    _searchController.searchBar.delegate = self;
    
}

-(void)loadData{
   
    self.contactsList=[NSMutableArray arrayWithCapacity:100];

    for (NSInteger i=0; i<100; i++) {
        [self.contactsList addObject:[NSString stringWithFormat:@"Tel:%00d",i]];
    }
}

#pragma mark - Table view data source

-(void)updateSearchResultsForSearchController:(nonnull UISearchController *)searchController{
    NSString *searchString = [self.searchController.searchBar text];
    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
    if (self.searchList!= nil) {
        [self.searchList removeAllObjects];
    }
    //过滤数据
    self.searchList= [NSMutableArray arrayWithArray:[_contactsList filteredArrayUsingPredicate:preicate]];
    
    //刷新表格
    SearchResultViewController *searchResultsViewController = (SearchResultViewController *)self.searchController.searchResultsController;
    searchResultsViewController.searchDataArray = [NSMutableArray arrayWithArray:self.searchList];
    [searchResultsViewController.tableView reloadData];
}

#pragma mark - UISearchBar的代理方法
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    NSLog(@"您点击了取消按钮");
    [searchBar resignFirstResponder]; // 丢弃第一使用者
}
#pragma mark - 实现键盘上Search按钮的方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    
    NSLog(@"您点击了键盘上的Search按钮");
    
      [self.searchController dismissViewControllerAnimated:YES completion:^{
        
        [self setup];
        
        NSLog(@"传值:uid===%@",self.resultVC.uid);
    }];
 
}


//自定义搜索取消button title
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    searchBar.showsCancelButton = YES;
    for (id cencelButton in [searchBar.subviews[0] subviews])
    {
        if([cencelButton isKindOfClass:[UIButton class]])
        {
            UIButton *btn = (UIButton *)cencelButton;
            [btn setTitle:@"取消"  forState:UIControlStateNormal];
        }
    }
  
}
@end

相关文章

网友评论

      本文标题:ios搜索栏之UISearchController

      本文链接:https://www.haomeiwen.com/subject/gvpvdttx.html