zoukankan      html  css  js  c++  java
  • 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.
    
    }
  • 相关阅读:
    flexbox 伸缩布局盒
    border-radius 知识点
    appium+Python第一个unitest
    linux常用命令整理
    appium的demo编程
    appium+Python环境搭建
    pycharm将py文件打包成可执行文件exe
    jmeter线程组设置
    jmeter的如何设置headers
    Python使用pillow的坑
  • 原文地址:https://www.cnblogs.com/WJJ-Dream/p/4983022.html
Copyright © 2011-2022 走看看