纯代码实现UISearchBar搜索功能
2013-12-17 21:04
330 查看
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];
}
运行结果:
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];
}
运行结果:
相关文章推荐
- swift 搜索框(UISearchBar)实现简单的搜索功能(swift3)
- UISearchBar, NSPredicate 实现简单的搜索功能
- UISearchbar 搜索按钮更换 代码实现UISearchbar去除第一响应
- swift 搜索框(UISearchBar)实现简单的搜索功能
- UITableView + UISearchBar 实现搜索功能
- iOS开发——UISearchController实现搜索栏功能|阳和移动开发
- GridView高效分页和搜索功能的实现代码
- Yii2.0小部件GridView(两表联查/搜索/分页)功能的实现代码
- 利用chosen.js插件实现下拉可搜索多选,控制选择个数,选项框复位,修改下拉选项的功能(附代码)
- JS 页面内容搜索,类似于 Ctrl+F功能的实现代码
- 使用系统控件UISearchBar实现APP中搜索功能
- easyUI实现类似搜索框关键词自动提示功能示例代码
- actionBar+SearchView实现搜索功能
- 如何实现简单的自动提示(autocomplete)填充搜索功能java代码。
- IOS-搜索功能实现, UIsearchController的使用
- iOS开发 - UISearchController实现搜索功能
- UISearchBar套接TableView实现搜索功能
- Vue.js实现实例搜索应用功能详细代码
- Vue 仿百度搜索功能实现代码