IOS:将搜索栏添加到表视图

来源:互联网 发布:数据过滤算法 编辑:程序博客网 时间:2024/04/29 15:29

  首先创建一个单视图工程,在ViewController.h中代码如下:

  

#import <UIKit/UIKit.h>


@interface ViewController :


UIViewController<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>


@property(retain,nonatomic)UITableView *mTableView;


@property(retain,nonatomic)UITableView *pMtableView;


@property(retain,nonatomic)UISearchBar *mSearcherBar;


@property(retain,nonatomic)NSMutableArray *mArr1;


@property(retain ,nonatomic)NSMutableArray *mArr2;


@end


  在ViewController.h中代码如下:

  

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

    //创建初始化一个表视图

    UITableView *pTable=[[UITableViewalloc]initWithFrame:self.view.framestyle:UITableViewStylePlain];

    

    self.mTableView = pTable;

    

    [pTable release];

    //创建初始化一个搜索框

    self.mSearcherBar = [[UISearchBaralloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,30)];

    //将表视图的表头设为搜索框

    self.mTableView.tableHeaderView =self.mSearcherBar;

    //设置代理

    self.mTableView.delegate =self;

    

    self.mTableView.dataSource =self;

    

    self.mSearcherBar.delegate =self;

    //将表视图添加到当前时图

    [self.viewaddSubview:self.mTableView];

    //初始化可变数组

    self.mArr1 = [[NSMutableArrayalloc]initWithCapacity:60];

    

    self.mArr2 = [[NSMutableArrayalloc]initWithCapacity:60];

    //给可变数组赋值

    for(int i=0;i<60;i++)

    {

        NSString *pstr=[NSStringstringWithFormat:@"%d",i];

        

        [self.mArr1addObject:pstr];

        

        [self.mArr2addObject:pstr];

    }

    

    

}


#pragma ------------------------DataSource Delegate----------------------------


//设置每行的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 50;

}

//设置每个分组表头的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

    return 20;

}

//设置每个分组表尾的高度

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

{

    return 20;

}


#pragma -----------------------------------UITableView Delegate------------------


//必须实现的两个协议

//返回每个分组包含的行数

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

{

    return [self.mArr2count];

}

//设置每行内容

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

{

    static NSString *pstr=@"hello";

    

    UITableViewCell *pCell=[tableViewdequeueReusableCellWithIdentifier:pstr];

    

    if(pCell == nil)

    {

        pCell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:pstr];

    }

    

    NSUInteger cellRow = [indexPath row];

    

    pCell.textLabel.text = [self.mArr2objectAtIndex:cellRow];

    

    pCell.textLabel.textColor = [UIColorgreenColor];

    

    pCell.detailTextLabel.text =@"detailTextField";

    

    pCell.detailTextLabel.textColor = [UIColorblackColor];

    

    return pCell;

}

//可以选择实现的协议

//设置分组数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 3;

}

//设置表头标题

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

{

    return@"Header";

}

//设置表尾的标题

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

{

    return@"Footer";

}


#pragma ---------------------------------UISearcherBar Delegate------------------


//在搜索框内搜索内容,并展示搜索结果

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar

{

    //移除mArr2中所有元素,来存放搜索结果

    [self.mArr2removeAllObjects];

    //对数组mArr1遍历,看数组中是否包含搜索框里的内容

    for(NSString *pstrinself.mArr1)

    {

        //如果包含搜索框里的内容,就把数组中的元素添加到mArr2

        if([pstr hasPrefix:searchBar.text])

        {

            [self.mArr2addObject:pstr];

        }

    }

    

    [self.mSearcherBarresignFirstResponder];

    //展示搜索结果

    [self.mTableViewreloadData];

    

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


 最后运行结果如下:



  在搜索框输入数字,比如2,点击search键,结果:




1 0
原创粉丝点击