UISearchbar的学习

今天学习UISearchbar 直接上代码

#import "ViewController.h"


@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchResultsUpdating,UISearchControllerDelegate>

@property (nonatomic,strong)UITableView *mainTable;

@property (nonatomic,strong)UISearchController *searchController;

@property (nonatomic,strong)NSMutableArray *dataList;

@property (nonatomic,strong)NSMutableArray *searchList;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [self searchControllerLayout];

}

//懒加载

-(NSMutableArray *)dataList

{

    if (_dataList == nil) {

        _dataList = [NSMutableArray arrayWithObjects:@"1",@"11",@"2",@"22",@"3",@"33",@"4",@"44", nil];

        

    }

    return _dataList;

}

//初始化searchController

-(void)searchControllerLayout{

    self.mainTable = [[UITableView alloc]initWithFrame:self.view.bounds];

    self.mainTable.delegate =self;

    self.mainTable.dataSource =self;

    

    

    // 初始化UISearchController

    self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];

    //输入提示框

    self.searchController.searchBar.placeholder = @"请输入商品名或者店铺";

    //设置searchResultsUpdater协议代理

    self.searchController.searchResultsUpdater =self;

    //设置searhControllerDelegate过程

    self.searchController.delegate =self;

    

    //设置UISearchController的显示属性,以下3个属性默认为YES

    //搜索时,背景变暗色

    //是否使背景颜色暗淡当陈叙的时候

    self.searchController.dimsBackgroundDuringPresentation = YES;

    //搜索时 背景变模糊

    self.searchController.obscuresBackgroundDuringPresentation = NO;

    //是否隐藏导航栏

    self.searchController.hidesNavigationBarDuringPresentation = NO;

    

    self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44);

    

    //更改系统自带的取消按钮 @“cancel” 变为取消

    [self.searchController.searchBar setValue:@"取消" forKey:@"cancelButtonText"];

    

    //searchbar添加到tableView的表头视图上

    self.mainTable.tableHeaderView =self.searchController.searchBar;

    //因为搜索是控制器,所以要使用模态推出(必须是模态,不可是push[selfpresentViewController:_searchController animated:YEScompletion:nil];

    

    [self.view addSubview:self.mainTable];

}

#pragma mark UItableView dataSource

//设置区域的行数

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    //将这个属性设置为是的是一个方便的方法来执行一个搜索的默认显示方式控制器适合控制器是如何配置的。实现-presentSearchController:如果默认的陈述是不够的。

    //应该是前者根本没有自己的表视图,它使用的表视图就是默认的表视图.也就是我们必须自默认的表视图中判断当前搜索是否激活,如果是则显示搜索后的结果,否则显示默认呈现的数据.

    

    //这一点, 可以通过UISearchControlleractive属性来判断, 即判断输入框是否处于active状态.

    if (self.searchController.active) {

        return self.searchList.count;

    }else{

        return self.dataList.count;

    }

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *cellIndentifile = @"UITablewViewCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifile];

    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifile];

    }

    //在这里要判断一下是否是UISearchbar 里面的TableView  这里用active 这个属性 是否活跃

    if (self.searchController.active) {

        cell.textLabel.text = self.searchList[indexPath.row];

    }else{

        cell.textLabel.text = self.dataList[indexPath.row];

    }

    

    return cell;

}

#pragma mark UISearchControllerDelegate

//测试UISearchControl的过程  一般用的少

-(void)willPresentSearchController:(UISearchController *)searchController

{

    NSLog(@"willPresentSearchController");

}

-(void)didPresentSearchController:(UISearchController *)searchController

{

     NSLog(@"didPresentSearchController");

}

   

-(void)willDismissSearchController:(UISearchController *)searchController

{

    NSLog(@"willDismissSearchController");

}

-(void)didDismissSearchController:(UISearchController *)searchController

{

    NSLog(@"didDismissSearchController");

}

-(void)presentSearchController:(UISearchController *)searchController

{

    NSLog(@"presentSearchController");

}

#pragma mark UISearchController  searchResultsUpdater协议

-(void)updateSearchResultsForSearchController:(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:[self.dataList filteredArrayUsingPredicate:preicate]];

    //刷新表格

    [self.mainTable reloadData];

}


//UISearchController的移除


//viewWillDisappear中要将UISearchController移除, 否则切换到下一个View, 搜索框仍然会有短暂的存在.



- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];

    if (self.searchController.active) {

        self.searchController.active = NO;

        [self.searchController.searchBar removeFromSuperview];

    }

}

UISearchController的使用步骤:

1、创建

//创建UISearchController
    _searchController = [[UISearchController alloc]initWithSearchResultsController:nil];

 

2、设置代理

    //设置代理
    _searchController.delegate = self;
    _searchController.searchResultsUpdater= self;

 

3、设置属性

    //设置UISearchController的显示属性,以下3个属性默认为YES
    //搜索时,背景变暗色
    _searchController.dimsBackgroundDuringPresentation = NO;
    //搜索时,背景变模糊
    _searchController.obscuresBackgroundDuringPresentation = NO;
    //隐藏导航栏
    _searchController.hidesNavigationBarDuringPresentation = NO;

 

4、实现代理

- (void)willPresentSearchController:(UISearchController *)searchController;
- (void)didPresentSearchController:(UISearchController *)searchController;
- (void)willDismissSearchController:(UISearchController *)searchController;
- (void)didDismissSearchController:(UISearchController *)searchController;
- (void)presentSearchController:(UISearchController *)searchController;

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController;

 

 

注意点:

1、如果你希望在同一个视图中显示搜索结果,则通过[[UISearchController alloc]initWithSearchResultsController:nil]。但是这是不支持TVOS,请提供TVOS一定要指定结果控制器。

[[UISearchController alloc]initWithSearchResultsController:VC],可以实现指定结果控制器。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值