您的位置:首页 > 移动开发 > IOS开发

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.

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息