UI之搜索框的创建Search

#import"RootViewController.h"

@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchResultsUpdating>

//引入三个代理,一个用于搜索,两个用于建立cell

@property(nonatomic,strong)UITableView * tableV;

@property(nonatomic,strong)UISearchController * searchCon;

@property(nonatomic,strong)NSMutableArray * dataArr;//存源数据

@property(nonatomic,strong)NSMutableArray * searchArr;//sear数据

@end

@implementationRootViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.title=@"搜索";

    self.searchArr=[NSMutableArraynew];

    [self createDataArr];

    [self createTable];

}

-(void)createDataArr

{

    self.dataArr=[NSMutableArraynew];

    for (int i=0; i<200; i++) {

        [self.dataArr addObject:[NSString stringWithFormat:@"%u",arc4random()%200]];

    }

}

-(void)createTable

{

    self.automaticallyAdjustsScrollViewInsets=NO;//

    self.tableV=[[UITableViewalloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) style:0];//设置表视图的范围

    self.tableV.delegate=self;

    self.tableV.dataSource=self;

    self.searchCon=[[UISearchControlleralloc]initWithSearchResultsController:nil];//创建搜索界面

    self.searchCon.searchResultsUpdater=self;//设置代理

    [self.searchCon.searchBarsizeToFit];//大小自适应,如果不设置则不显示搜索框

    self.searchCon.hidesNavigationBarDuringPresentation=NO;//点击搜索杠后是否隐藏导航条

    self.searchCon.dimsBackgroundDuringPresentation=NO;//点击搜索框后是否显示半透明灰色背景

    self.searchCon.searchBar.placeholder=@"请输入关键字....";//设置提示文字

    self.tableV.tableHeaderView=self.searchCon.searchBar;//把搜索条设为tableView的表头searchBarsearchController的属性,是一个搜索条

    [self.view addSubview:self.tableV];

}

//UISearchResultsUpdating协议方法,点击searchBar会触发这个方法

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController

{

    [self.searchArr removeAllObjects];//把数组清空(删除上一次搜索的结果)

    NSPredicate* predicate=[NSPredicate predicateWithFormat:@"SELFCONTAINS [c]%@",searchController.searchBar.text];//SELF CONTAINS[c]%@检索searchBar.text

   

    NSArray* array=[self.dataArrfilteredArrayUsingPredicate:predicate];//根据predicate进行检索(遍历查询)

    [self.searchArr addObjectsFromArray:array];//把检索出来的数据存在searchArr

    [self.tableV reloadData];

}

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

{

    if (self.searchCon.active){//active活跃状态是否进入搜索状态

        returnself.searchArr.count;

    }

    return self.dataArr.count;

}

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

{

    static NSString * strid=@"id";

    UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:strid];

    if(cell==nil) {

        cell=[[UITableViewCellalloc]initWithStyle:0 reuseIdentifier:strid];

    }

    if (self.searchCon.active) {//显示搜索后的结果

        cell.textLabel.text=[NSString stringWithFormat:@"%@",self.searchArr[indexPath.row]];

    }else

    {//显示全部的结果

        cell.textLabel.text=[NSString stringWithFormat:@"%@",self.dataArr[indexPath.row]];

    }

    returncell;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值