iOS 搜索栏的使用
2015-10-29 14:29
603 查看
最近写项目的时候用到了UISearchBar和UISearchDisplayController,就整理了下。
UISearchDisplayController(搜索结果显示控制器(内部有一个 tableView经常和UISearchBar一起使用))是iOS8之前的,通常是显示搜索结果的显示器。
UISearchBar 搜索条,一般是UITableView的头部视图,然后在定义两个数组,一个是数据源数组,一个是搜索结果的数组。从数据元数组中便利找到与要搜索的内容一样的放在搜索结果的数组中。
UISearchBar这个控件就用来搜索tableView上的数据。
[[UISearchDisplayController
alloc] initWithSearchBar:searchBar
contentsController:self]; 第1个参数:关联的 搜索条 这样 一点击 搜索条 那么就会触发UISearchDisplayController。第二个参数搜索 到结果之后_disPlayC的view显示在 那个视图控制器上 self 显示在当前视图控制器上。UISearchDisplayController内部有一个tableView
所以要设置数据源和代理。现在这里有两个tableView 下面的tableView协议的方法 要判断是哪一个tableView。
当点击searchBar时,它会自动上移并且遮住navigationController经过测试,如果隐藏导航条则不会移动。
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UISearchDisplayController *searchDis;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableArray *resultArr;
@end
ViewController.m
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//[self.navigationController setNavigationBarHidden:YES animated:YES];
//self.automaticallyAdjustsScrollViewInsets = NO;
self.dataArr = [[NSMutableArray alloc] init];
self.resultArr = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < 26; i++) {
NSString *str = [NSString stringWithFormat:@"请输入%ld",i];
[self.dataArr addObject:str];
}
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height - 64 - 49) style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.rowHeight = 45;
[self.view addSubview:self.tableView];
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
self.tableView.tableHeaderView = self.searchBar;
self.searchDis = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchDis.searchResultsDataSource = self;
self.searchDis.searchResultsDelegate= self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView != self.tableView) {
//NSArray *arr = [NSArray arrayWithArray:self.dataArr];
[self.resultArr removeAllObjects];
for (NSString *str in self.dataArr) {
NSRange range = [str rangeOfString:self.searchBar.text];
if (range.location != NSNotFound) {
[self.resultArr addObject:str];
}
}
return self.resultArr.count;
}
return self.dataArr.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView != self.tableView) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"a"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"aa"];
}
cell.textLabel.text = [self.resultArr objectAtIndex:indexPath.row];
return cell;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"a"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"aa"];
}
cell.textLabel.text = [self.dataArr objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailViewController *detail = [[DetailViewController alloc] init];
[self.navigationController pushViewController:detail animated:YES];
}
- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (tableView != self.tableView) {
return @"搜索结果";
}
return nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
UISearchDisplayController(搜索结果显示控制器(内部有一个 tableView经常和UISearchBar一起使用))是iOS8之前的,通常是显示搜索结果的显示器。
UISearchBar 搜索条,一般是UITableView的头部视图,然后在定义两个数组,一个是数据源数组,一个是搜索结果的数组。从数据元数组中便利找到与要搜索的内容一样的放在搜索结果的数组中。
UISearchBar这个控件就用来搜索tableView上的数据。
[[UISearchDisplayController
alloc] initWithSearchBar:searchBar
contentsController:self]; 第1个参数:关联的 搜索条 这样 一点击 搜索条 那么就会触发UISearchDisplayController。第二个参数搜索 到结果之后_disPlayC的view显示在 那个视图控制器上 self 显示在当前视图控制器上。UISearchDisplayController内部有一个tableView
所以要设置数据源和代理。现在这里有两个tableView 下面的tableView协议的方法 要判断是哪一个tableView。
当点击searchBar时,它会自动上移并且遮住navigationController经过测试,如果隐藏导航条则不会移动。
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UISearchDisplayController *searchDis;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableArray *resultArr;
@end
ViewController.m
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//[self.navigationController setNavigationBarHidden:YES animated:YES];
//self.automaticallyAdjustsScrollViewInsets = NO;
self.dataArr = [[NSMutableArray alloc] init];
self.resultArr = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < 26; i++) {
NSString *str = [NSString stringWithFormat:@"请输入%ld",i];
[self.dataArr addObject:str];
}
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height - 64 - 49) style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.rowHeight = 45;
[self.view addSubview:self.tableView];
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
self.tableView.tableHeaderView = self.searchBar;
self.searchDis = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchDis.searchResultsDataSource = self;
self.searchDis.searchResultsDelegate= self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView != self.tableView) {
//NSArray *arr = [NSArray arrayWithArray:self.dataArr];
[self.resultArr removeAllObjects];
for (NSString *str in self.dataArr) {
NSRange range = [str rangeOfString:self.searchBar.text];
if (range.location != NSNotFound) {
[self.resultArr addObject:str];
}
}
return self.resultArr.count;
}
return self.dataArr.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView != self.tableView) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"a"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"aa"];
}
cell.textLabel.text = [self.resultArr objectAtIndex:indexPath.row];
return cell;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"a"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"aa"];
}
cell.textLabel.text = [self.dataArr objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailViewController *detail = [[DetailViewController alloc] init];
[self.navigationController pushViewController:detail animated:YES];
}
- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (tableView != self.tableView) {
return @"搜索结果";
}
return nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 搜狗百度360市值齐跌:搜索引擎们陷入集体焦虑?
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 本人即将筹备败家日志,敬请期待!
- IE:使用搜索助手
- C++深度优先搜索的实现方法
- 基于文本的搜索
- php实现搜索一维数组元素并删除二维数组对应元素的方法
- 使用Sphinx对索引进行搜索
- asp 多关键词搜索的简单实现方法
- C#使用foreach语句搜索数组元素的方法
- JavaScript中数组的排序、乱序和搜索实现代码
- C#编程实现Excel文档中搜索文本内容的方法及思路
- sqlserver中在指定数据库的所有表的所有列中搜索给定的值
- 可以用来搜索当前页面内容的js代码