美文网首页UITableView
UISearchController的简单应用

UISearchController的简单应用

作者: Mustard_Buli | 来源:发表于2016-03-16 17:05 被阅读234次
    @interface ViewController ()<UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating>
    @property (nonatomic, strong) NSMutableArray *dataSourceArray;
    @property (nonatomic, strong) UITableView *myTableView;
    @property (nonatomic, strong) UISearchController *searchController;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self initDataSource];
        
        self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        [self.view addSubview:_myTableView];
        
        //创建searchController
        SearchResultViewController *sResultCtrl = [[SearchResultViewController alloc] init];
        //告诉这个sCtrl 搜索之后的结果就显示在当前这个控制器上
        self.searchController = [[UISearchController alloc] initWithSearchResultsController:sResultCtrl];
        //由谁来监听searchbar上面的搜索内容发生变化的事件
        _searchController.searchResultsUpdater = self;
        //在tableView的头部显示一个searchBar
        _myTableView.tableHeaderView = _searchController.searchBar;
        //给新的界面提供一个展示的上下文
        _searchController.definesPresentationContext = YES;
    }
    
    //准备数据源
    - (void)initDataSource{
        self.dataSourceArray = [NSMutableArray array];
        
        //A1 A2 A3 A4 -Z4
        for (int i = 0; i < 26; i++) {
            for (int j = 0; j < 4; j++) {
                NSString *alpha = [NSString stringWithFormat:@"%c%d",'A'+i, j+1];
                
                [_dataSourceArray addObject:alpha];
            }
        }
    }
    
    #pragma mark -- DataSource&Delegate
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 26;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return 4;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        NSString *cellID = @"CellID";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        
        cell.textLabel.text = [_dataSourceArray objectAtIndex:indexPath.section*4 + indexPath.row];
        
        return cell;
    }
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
        return [NSString stringWithFormat:@"%c", 'A'+(int)section];
    }
    
    //索引列表的标题
    - (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
        NSMutableArray *titlesArray = [NSMutableArray array];
        
        for (int i = 0; i < 26; i++) {
            [titlesArray addObject:[NSString stringWithFormat:@"%c",'A'+i]];
        }
        return titlesArray;
    }
    
    #pragma mark --- searchResultUpdater
    //监听searchBar上面内容的变化
    - (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
        //过滤 查找
        //定义过滤条件 只要包含搜索的内容就过滤出来
        //beginWith endWith like constains
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchController.searchBar.text];
        
        //开始过滤
        NSArray *resutls = [_dataSourceArray filteredArrayUsingPredicate:predicate];
        
        //将过滤的内容显示
        SearchResultViewController *sResultCtrl = (SearchResultViewController *)_searchController.searchResultsController;
        
        sResultCtrl.dataSourceArray = resutls;
    }
    @end
    

    相关文章

      网友评论

        本文标题:UISearchController的简单应用

        本文链接:https://www.haomeiwen.com/subject/hvgqlttx.html