纯代码实现UISearchBar搜索功能

来源:互联网 发布:java 读取cad文件 dwg 编辑:程序博客网 时间:2024/05/22 18:30

2013年12月17日学习日志:UISearchBar

UISearchBar是一种搜索栏,这次我主要是写一些今天课上学到的一些有关searchBar的内容;

即怎样代码创建一个搜索栏,怎样使搜索栏弹出的键盘消失,怎样使得达到搜索想要的内容并显示在表视图中。

1.    File-àNew-àProject创建一个Empty Application,工程命名为mySearch;

2.    在AppDelegate.h中包含头文件#import”HXViewController.h”,声明一个它的属性视图控制器。

@property (retain,nonatimic) HXViewController*viewController;

在.m文件的dealloc方法中release  掉viewController;

- (void)dealloc

{

  [_window release];

  [_biewControllerrelease];

  [super dealloc];

}

3.在AppDlegate.m文件中的-(BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加代码,使viewController成为根视图控制器:

   HViewController*pTempVC =

   [[HXViewController  alloc]initWithNibName:nil bundle:nil];

   self.viewController= pTempVC;

   [pTempVC release];

   self.window.rootViewController= self.viewController;

4.在HXViewController.h中添加一个UITableView对象和一个UISearchBar对象:

#import <UIKit/UIKit.h>

 

@interface HXViewController : UIViewController

@property (retain,nonatomic)UITableView *mTableView;

@property (retain,nonatomic)UISearchBar *mSearchBar;

 

@end

并在.m中重写父类的dealloc方法

- (void)dealloc

{

    [_mTableViewrelease];

    [_mSearchBarrelease];

    [super dealloc];

}

5.在HXViewController.m的-(void)viewDidLoad方法中添加代码:

  - (void)viewDidLoad

{

    [super viewDidLoad];

    //初始化表视图

    self.mTableView =[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

    //设置myTableView的代理为自己

   self.mTableView.dataSource = self;

    //searchBar的初始化

    self.mSearchBar =[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0,self.mTableView.frame.size.width, 30)];

    //设置mSearchBar的代理为自己

   self.mSearchBar.delegate = self;

    //设置当前表视图的头视图

   self.mTableView.tableHeaderView = self.mSearchBar;

    //把当前创建的mTableView加载到视图中

    [self.view addSubview:self.mTableView];

}

6.因为要实现表视图的加载必须遵守UITableDataSourse协议,并且协议中有两个required方法,即必须实现的方法;下面我们着重来实现这两个方法:

 实现这两个方法前先在.h中添加协议声明:

@interface HXViewController: UIViewController<UITableViewDataSource,UISearchBarDelegate>

因为在待会下面要实现搜索栏的功能,要用到UISearchBarDelegate协议,所以这次就一块写下来了;

  #pragma mark----tableView dataSource-----

//返回每个section里面的段数

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

{

    return 10;//一页视图显示10行

}

 

//渲染每行

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

{

    //一个静态字符串做标签(静态标签防止局部变量多次创建,提高效率,节约内存)

    static NSString *identifier =@"identifier";

    //创建一个cell对象(利用表视图可以复用的机制)

    UITableViewCell *pCell = [tableViewdequeueReusableCellWithIdentifier:identifier];

    //如果cell为空,就创建一个新的出来

    if (nil == pCell)

    {

        pCell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];

    }

    //设置cell的基本属性

    pCell.textLabel.text = @"nihao";

    return pCell;

}

7.添加搜索栏功能,实现搜索,并且键盘可以消失;

   - (void)viewDidLoad

{

    [super viewDidLoad];

    //初始化表视图

    self.mTableView =[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

    //设置myTableView的代理为自己

    self.mTableView.dataSource= self;

    //searchBar的初始化

    self.mSearchBar =[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0,self.mTableView.frame.size.width, 30)];

    //设置mSearchBar的代理为自己

    self.mSearchBar.delegate =self;

    //设置当前表视图的头视图

   self.mTableView.tableHeaderView = self.mSearchBar;

    //把当前创建的mTableView加载到视图中

    [self.viewaddSubview:self.mTableView];

    //数组内容初始化,给数组分配60个对象

    self.mArr1 =[[NSMutableArray alloc]initWithCapacity:60];

    self.mArr2 =[[NSMutableArray alloc]initWithCapacity:60];

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

    {

        //使数组内容为0到59

        NSString *pTempStr =[NSString stringWithFormat:@"%d",i];

        [self.mArr1addObject:pTempStr];

        [self.mArr2addObject:pTempStr];

    }

}

 

#pragma mark----tableView dataSource-----

//返回每个section里面的段数

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

{

    //return 10;//一页视图显示10行

    return [self.mArr2 count];

}

//渲染每行

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

{

    //一个静态字符串做标签(静态标签防止局部变量多次创建,提高效率,节约内存)

    static NSString*identifier = @"identifier";

    //创建一个cell对象(利用表视图可以复用的机制)

    UITableViewCell *pCell =[tableView dequeueReusableCellWithIdentifier:identifier];

    //如果cell为空,就创建一个新的出来

    if (nil == pCell)

    {

        pCell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:identifier];

    }

    //设置cell的基本属性

    //pCell.textLabel.text =@"nihao";

    //得到当前行

    NSInteger mRow =[indexPath row];

    //使得当前行的标题为当前的行数

    pCell.textLabel.text =[self.mArr2 objectAtIndex:mRow];

    return pCell;

}

 

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar

{

    //之前表视图中显示的是所有的内容,要实现搜索功能,就要先清除先前的,使其只显示搜索到的内容

    [self.mArr2removeAllObjects];

   

    for (NSString *str inself.mArr1)

    {

        if ([strhasPrefix:searchBar.text])

        {

            [self.mArr2addObject:str];

        }

    }

    //使mSearchView放弃第一响应者,从而达到退出键盘的功能

    [self.mSearchBarresignFirstResponder];

    //重新加载表视图

    [self.mTableViewreloadData];

}


运行结果:

0 0
原创粉丝点击