iOS 搜索栏的使用
来源:互联网 发布:斐讯路由器 淘宝 编辑:程序博客网 时间:2024/05/18 02:03
最近写项目的时候用到了UISearchBar和UISearchDisplayController,就整理了下。
UISearchDisplayController(搜索结果显示控制器(内部有一个 tableView经常和UISearchBar一起使用))是iOS8之前的,通常是显示搜索结果的显示器。
UISearchBar 搜索条,一般是UITableView的头部视图,然后在定义两个数组,一个是数据源数组,一个是搜索结果的数组。从数据元数组中便利找到与要搜索的内容一样的放在搜索结果的数组中。
UISearchBar这个控件就用来搜索tableView上的数据。
[[UISearchDisplayControlleralloc] initWithSearchBar:searchBarcontentsController: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.
}
- iOS 搜索栏的使用
- IOS-搜索功能实现, UIsearchController的使用
- IOS开发----UISearchBar,搜索框的使用
- ios搜索框功能的实现/UISearchBar/UISearchController的使用
- iOS 8 NSUserActivity和iOS9 CoreSpotlight 搜索API的使用
- iOS 搜索条使用详解
- iOS搜索栏简介
- iOS 导航栏 搜索
- 搜索图标居中的搜索框~iOS风格搜索框
- ios UITableView表格实现搜索栏搜索
- iOS 搜索功能的实现
- iOS搜索框的实现
- iOS 搜索功能的实现
- iOS 搜索功能的实现
- iOS 模糊搜索的实现
- iOS搜索框的实现
- ios搜索栏的设置及搜索栏的背景图的设置
- NSPredicate和搜索栏的使用
- Mina框架学习笔记(一)
- 欢迎使用CSDN-markdown编辑器
- 向热爱装逼的你推荐clojurescript
- 携程景点门票api演示示例分享
- getOutputStream() has already been called for this response 错误异常的处理
- iOS 搜索栏的使用
- Mina框架学习笔记(二)
- 树莓派外接USB摄像头系列三
- Maven搭建hadoop环境之pom.xml基本配置
- MySQL中如何将一个数据库的表导入到另一个数据库表中
- android的listView组件
- Virtualbox WDDM之DxgkDdiSetPointerShape
- synchronized详解
- Android 任务和返回栈完全解析,细数那些你所不知道的细节