UISearchDisplayController和UISearchBar

来源:互联网 发布:赌博庄家抽水算法 编辑:程序博客网 时间:2024/05/29 18:55

用UISearchDisplayController来作为搜索控制器(iOS8以后用UISearchController),以下是写了一个demo,简单的介绍了一下UISearchDisplayController的用法和UISearchBar的用法.

#import "JLROOTViewController.h"@interface JLROOTViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchDisplayDelegate>@end@implementation JLROOTViewController{    UITableView *_tabview;    UISearchBar *_searchbar;// 搜索框    NSMutableArray *_dataArry;// 数据源    NSMutableArray *_searchdata;// 搜索数据    UISearchDisplayController *_SDcv;// 搜索控制器,自带一个tableview//    UISearchController(ios8.0 later)}- (void)viewDidLoad {    [super viewDidLoad];        [self createData];    [self createUI];}- (void)createData{    _dataArry = [[NSMutableArray alloc] init];    for (int i= 'A'; i<'Z'; i++) {        NSMutableArray *smallarray = [[NSMutableArray alloc] init];        for (int j=0; j <10; j++) {            NSString *str = [NSString stringWithFormat:@"%c%c%c%c%c",i,arc4random()%26+'A',arc4random()%26+'A',arc4random()%26+'A',arc4random()%26+'A'];            [smallarray addObject:str];        }        [_dataArry addObject:smallarray];    }    _searchdata = [[NSMutableArray alloc] init];}- (void)createUI{    _tabview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];    _tabview.delegate = self;    _tabview.dataSource =self;    [self.view addSubview:_tabview];    // searchbar    _searchbar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];    self.navigationItem.titleView = _searchbar;    _searchbar.barStyle = UIBarStyleBlack;    _searchbar.keyboardType = UIKeyboardAppearanceAlert;    _searchbar.placeholder = @"请输入查找内容";        _searchbar.showsBookmarkButton = YES;    _searchbar.showsSearchResultsButton = YES;    _searchbar.showsCancelButton = YES;        _SDcv = [[UISearchDisplayController alloc] initWithSearchBar:_searchbar contentsController:self];        // 这两个代理是UISearchDisplayController自带的tableview遵循的tableview的代理    _SDcv.searchResultsDelegate = self;    _SDcv.searchResultsDataSource = self;        // 遵循搜索代理    _SDcv.delegate = self;        // 这个设置是让搜索栏的tab在NavigationBar下显示    _SDcv.displaysSearchBarInNavigationBar = YES;}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    if (tableView == _tabview) {        return _dataArry.count;    }        return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    if ([tableView isEqual:_tabview]) {        return [_dataArry[section] count];    }else{        [_searchdata removeAllObjects];        // 搜索        for (NSArray *arr in _dataArry) {            for (NSString *str in arr) {                // 查找数据源是否有查找的str                NSRange range = [str rangeOfString:_searchbar.text];                if (range.location != NSNotFound) {                    [_searchdata addObject:str];                }            }        }        return _searchdata.count;    }}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *str = @"cellid";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];    if (!cell) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];    }    if (tableView == _tabview) {         cell.textLabel.text = _dataArry[indexPath.section][indexPath.row];    }else{        // searchtab        cell.textLabel.text = _searchdata[indexPath.row];    }        return cell;}- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    if (tableView == _tabview) {        return [NSString stringWithFormat:@"%c组",'A'+(int)section];    }    return @"搜索结果";}

0 0