UISearchBar搜索控制器

来源:互联网 发布:windows xp镜像下载 编辑:程序博客网 时间:2024/06/10 00:09


<UITableViewDataSource,UITableViewDelegate>两个协议

 

全局变量

 

{

 //表格式图

    UITableView * _tableView;

    //搜索条

    UISearchBar* _searchBar;

    //搜索控制器

    UISearchDisplayController * _seachDisplay;

    //展示的数据

    NSMutableArray * dataArray;

    //展示搜索的数据源

    NSMutableArray * resultArray;

 

 

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    [self initWithData];

    [self createSearch];

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

}

-(void)initWithData

{

    dataArray=[[NSMutableArray alloc]init];

    

    resultArray=[[NSMutableArray alloc]init];

    

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

    {

        NSMutableArray* subArray=[NSMutableArray array];

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

        {

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

            [subArray addObject:string];

        }

        [dataArray addObject:subArray];

    }

    

}

#pragma mark -----搜索方法-----

-(void)createSearch

{

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

    

    _tableView.dataSource=self;

    

    _tableView.delegate=self;

    

    [self.view addSubview:_tableView];

 

    //创建搜索条

    

    _searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(00self.view.frame.size.width44)];

    

    //将其添加到表格头上

    _tableView.tableHeaderView=_searchBar;

    

    //创建搜索控制器

    _seachDisplay=[[UISearchDisplayController alloc]initWithSearchBar:_searchBar contentsController:self];

    

    //其上边自带了一个表格式图

    _seachDisplay.searchResultsDelegate=self;

    

    _seachDisplay.searchResultsDataSource=self;

    

    

 

}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    

    if (tableView!=_tableView) {

        return @"搜索结果";

    }

    

    

    

    return [NSString stringWithFormat:@"%c",(unichar)('A'+section)];

    

    

}

-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView

{

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

    //添加小标志

    [title addObject:UITableViewIndexSearch];

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

        [title addObject:[NSString stringWithFormat:@"%c",i]];

    }

    return title;

}

//设置一下对应关系

-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index

{

    //index 0 -26 搜索-Z 分组0-25

    if (index==0) {

        //点击搜索条时 tableView 滑到搜索条的位置

        [_tableView scrollRectToVisible:_searchBar.frame animated:YES];

        return -1;

    }

    return index-1;

    

}

 

#pragma mark -----协议方法-----

 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    //搜索控制器上自带的表格式图

    if (tableView!=_tableView)

    {

        return 1;

    }

    

    return dataArray.count;

 

}

 

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

{

    if (tableView!=_tableView)

    {

        //返回搜索结果的值

        //现将之前搜索的内容 移除

        [resultArray removeAllObjects];

        

        for (NSArray * subArray in dataArray)

        {

            for (NSString * string in subArray)

            {

                NSRange range=[string rangeOfString:_searchBar.text];

                

                if (range.location!=NSNotFound)

                {

                    [resultArray addObject:string];

                }

            }

        }

        

        return resultArray.count;

        

    }

    

    return [[dataArray objectAtIndex: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 (tableView!=_tableView)

    {

        

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

        

        

    }else

    {

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

 

    }

 

    

    

    return cell;

 

}

0 0
原创粉丝点击