UISearchController

来源:互联网 发布:淘宝上有卖岛国片cd 编辑:程序博客网 时间:2024/04/29 07:52


<UITableViewDataSource,UITableViewDelegate,UISearchResultsUpdating>代理

全局变量

{

    //表格式图

    UITableView * _tableVIew;

    //搜索控制器

    

    UISearchController * _search;

    //存放展示的数据

    NSMutableArray * dataArray;

    //存放搜索结果的数据

    NSMutableArray * resultArray;

 

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    [self initWithData];

    

    [self createSearchView];

    

    

}

-(void)initWithData

{

    dataArray=[NSMutableArray array];

    resultArray=[NSMutableArray array];

    for (int i='A'; i<='Z'; i++) {

        NSMutableArray * subArray=[[NSMutableArray alloc]init];

        for (int j=0; j<10; j++) {

            NSString * string=[NSString stringWithFormat:@"%c %d",i,j];

            [subArray addObject:string];

        }

        [dataArray addObject:subArray];

    }

    

 

}

//创建搜索界面

-(void)createSearchView

{

    //创建表格式图

    _tableVIew=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];

    

    _tableVIew.dataSource=self;

    

    _tableVIew.delegate=self;

    

    [self.view addSubview:_tableVIew];

    

    //创建搜索控制器

    //这个事对模态条状准备的置为nil 即可

    _search=[[UISearchController alloc]initWithSearchResultsController:nil];

    //设置代理

    _search.searchResultsUpdater=self;

    //3 设置字体大小

    

    [_search.searchBar sizeToFit];

    

    //4 搜索时是否将背景置暗

    _search.dimsBackgroundDuringPresentation=YES;

    

    //5 搜索时是否将导航栏隐藏

    _search.hidesNavigationBarDuringPresentation=NO;

    

    //将搜索条置为表格的头视图

    _tableVIew.tableHeaderView=_search.searchBar;

    

    

    

 

}

 

#pragma mark -----表格的协议方法-----

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    //有搜索结果时候返回一个分组

    if (resultArray.count!=0)

    {

        return 1;

    }

    

    return  [dataArray count];

    

}

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

{

    if (resultArray.count!=0)

    {

        return  [resultArray count];

    }

    

    return [dataArray [section] count];

 

}

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

{

    static NSString * cellId=@"cellId";

    

    UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:cellId];

    

    if (cell==nil)

    {

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

        

    }

    

    if (resultArray.count!=0)

    {

        cell.textLabel.text=resultArray[indexPath.row];

    }else

    {

        cell.textLabel.text=dataArray[indexPath.section][indexPath.row];

    

    }

    

    

    

    return cell;

 

}

 

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController

{

    //拿到搜索框内容

    NSString* searchText=searchController.searchBar.text;

    //将之前的数据删除

    [resultArray removeAllObjects];

    

    //便利展示的数据

    for (NSArray * subArray in dataArray)

    {

        for (NSString * string in subArray)

        {

            

            NSRange range=[string rangeOfString:searchText];

            

            if (range.location!=NSNotFound) {

                [resultArray addObject:string];

            }

        }

    }

    //刷新数据

    [_tableVIew reloadData];

}

0 0
原创粉丝点击