• iOS-UISearchController用法


    import "ViewController.h"
    @interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,UISearchResultsUpdating>
    @property (strong,nonatomic) NSMutableArray  *dataList;
    @property (strong,nonatomic) NSMutableArray  *searchList;
    @property (nonatomic, strong) UISearchController *searchController;
    @end
    @implementation ViewController
    {
    
        UITableView * _tableView;
    
    }
    
    - (void)viewDidLoad {
    
        [super viewDidLoad];
    
        _dataList = [NSMutableArray  array];
    
        _searchList = [NSMutableArray array];
    
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height - 44) style:UITableViewStylePlain];
    
        _tableView.delegate = self;
    
        _tableView.dataSource = self;
    
        [self.view addSubview:_tableView];
    
        _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    
        _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);
    
       _tableView.tableHeaderView = self.searchController.searchBar;
        
        self.dataList=[NSMutableArray arrayWithCapacity:100];
        
        for (NSInteger i=0; i<100; i++) {
    
            [self.dataList addObject:[NSString stringWithFormat:@"%ld-FlyElephant",(long)i]];
    
        }
    
    }
     
    //设置区域
    
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
        return 1;
    
    }
    
    //设置区域的行数
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
        if (self.searchController.active) {
    
            return [self.searchList count];
    
        }else{
    
            return [self.dataList count];
    
        }
    
    }
    
    //返回单元格内容
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        static NSString *flag=@"cellFlag";
    
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag];
    
        if (cell==nil) {
    
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag];
    
        }
    
        if (self.searchController.active) {
    
            [cell.textLabel setText:self.searchList[indexPath.row]];
    
        }
    
        else{
    
            [cell.textLabel setText: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]];
    
        //刷新表格
    
        [_tableView reloadData];
    
    }- (void)didReceiveMemoryWarning {
    
        [super didReceiveMemoryWarning];
    
        // Dispose of any resources that can be recreated.
    
    }
  • 相关阅读:
    关于mysql数据库引擎MyIsam的表锁理解
    关于mysql数据库引擎InnoDB事务的表锁和行锁理解
    Linux查看端口、进程情况及kill进程
    linux 用yum安装软件和卸载软件
    VS 2017常用快捷键
    cin作为判断条件时(关于cin的理解)
    uva 101 木块问题
    Uva 10474 sort以及lower_bound的用法
    团队项目-选题报告
    第一次结对编程作业
  • 原文地址:https://www.cnblogs.com/WJJ-Dream/p/4983022.html
Copyright © 2020-2023  润新知