iOS 用UISearchController自定义搜索界面

现今,商城类项目、O2O项目一抓一大把,而它们都必不可少的功能,就是搜索功能,因为iOS8以后用UISearchController代替了UIsearchDisplayController,所以我专门用UISearchController写了一个搜索模板,希望可以帮到需要的朋友。

话不多少,直接上代码,都有注释的。


遵循代理方法

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,UISearchResultsUpdating>

@property (nonatomic,strong)UITableView *mainTable;

@property (nonatomic,strong)UISearchController *searchController;

@property (nonatomic,strong)NSMutableArray *dataList;
@property (nonatomic,strong)NSMutableArray *searchList;

@end


- (void)viewDidLoad {
    [super viewDidLoad];

    _dataList = [NSMutableArray arrayWithObjects:@"1",@"11",@"2",@"22",@"#",@"3",@"33",@"4",@"44",@"ff",@"a",@"aaa",@"555",@"666",nil];

    [self searchControllerLayout];

}

-(void)searchControllerLayout{

    self.mainTable = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];

    self.mainTable.delegate = self;
    self.mainTable.dataSource = self;

    [self.view addSubview:self.mainTable];

    _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
//    输入框提示
    _searchController.searchBar.placeholder=@"请输入商品名或商铺名";
//    searchResultsUpdater协议代理
    _searchController.searchResultsUpdater = self;
//    使之背景暗淡当陈述时
    _searchController.dimsBackgroundDuringPresentation = NO;
//    隐藏导航栏
    _searchController.hidesNavigationBarDuringPresentation = NO;
//    
    _searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
//   改变系统自带的“cancel”为“取消”
    [self.searchController.searchBar setValue:@"取消" forKey:@"_cancelButtonText"];
//   表头视图为searchcontroller的searchbar
    self.mainTable.tableHeaderView = self.searchController.searchBar;

}



实现代理方法

//设置区域的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.searchController.active) {
        return _searchList.count;
    }else{
        return _dataList.count;
    }
}

//返回单元格内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *identifier=@"cellFlag";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    if (self.searchController.active) {

        cell.textLabel.text = self.searchList[indexPath.row];
    }
    else{
        cell.textLabel.text = self.dataList[indexPath.row];

    }
    return cell;
}

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {

    NSString *searchString = [self.searchController.searchBar text];
    //谓词过滤
    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
    if (self.searchList!= nil) {
        [self.searchList removeAllObjects];
    }
    //过滤数据
    self.searchList= [NSMutableArray arrayWithArray:[_dataList filteredArrayUsingPredicate:preicate]];
    //刷新表格
    [self.mainTable reloadData];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.searchController.active) {
        NSLog(@"%@被选中",_searchList[indexPath.row]);
    }else{
        NSLog(@"%@被选中",_dataList[indexPath.row]);
    }

}

实现效果如下:
iOS 用UISearchController自定义搜索界面_第1张图片


iOS 用UISearchController自定义搜索界面_第2张图片




iOS 用UISearchController自定义搜索界面_第3张图片

如果有什么写的不对的,欢迎大家指正。

你可能感兴趣的:(ios,objective-c)