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.
    
    }
  • 相关阅读:
    【struts1】--Servlet讲解MVC框架基本原理
    bigDecimal使用方法详解(转载)
    【web】--Ajax data选择性赋值
    【JSTL】--格式化日期--drp217
    2021-01-01:https加解密机制,你了解多少?
    2020-12-31:tcp三次握手,最后一次失败,网络会怎么样?
    2020-12-30:生产环境 CPU 占用过高,你如何解决?
    2020-12-29:mysql中,innodb表里,某一条数据删除了之后,这条数据会被真实的擦掉吗,还是删除了关系?
    2020-12-28:java中,生产环境服务器变慢,如何诊断处理?
    2020-12-27:网络调试工具都用了什么?
  • 原文地址:https://www.cnblogs.com/WJJ-Dream/p/4983022.html
Copyright © 2011-2022 走看看