zoukankan      html  css  js  c++  java
  • search搜索

    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchResultsUpdating>
    @property(nonatomic,strong) NSMutableArray * datalist;
    @property(nonatomic,strong) NSMutableArray * searchList;
    @property(nonatomic,strong) UITableView * tableView;
    @property (nonatomic, strong) UISearchController *searchController;
    @end

    @implementation ViewController
    -(UITableView *)tableView {
        if (!_tableView) {
            _tableView=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        }
       
        return _tableView;
       
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.datalist = [NSMutableArray arrayWithCapacity:100];
        for (NSUInteger i=0; i<100; i++) {
            [self.datalist addObject:[NSString stringWithFormat:@"%ld -FlyElephant",(long)i]];
        }
       
        _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);
        self.tableView.tableHeaderView = self.searchController.searchBar;
        [self addTableView];
    }
    -(void)addTableView{
       
       
        self.tableView.delegate =self;
        self.tableView.dataSource =self;
        [self.view addSubview:self.tableView];
    }
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
       
        if (self.searchController.active) {
            return [self.searchList count];
        }else{
            return [self.datalist count];
        }
    }
    -(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
       
       
        return YES;
    }
    -(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
       
       
        NSLog(@"结束");
        return YES;
    }
    -(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;
    }
    //- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
    //    // 谓词的包含语法,之前文章介绍过http://www.cnblogs.com/xiaofeixiang/
    //    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
    //    if (self.searchList!= nil) {
    //        [self.searchList removeAllObjects];
    //    }
    //    //过滤数据
    //    self.searchList= [NSMutableArray arrayWithArray:[_datalist filteredArrayUsingPredicate:preicate]];
    //    //刷新表格
    //    return YES;
    //}
    -(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.tableView reloadData];
    }
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
       
        if (self.searchController.active) {
            NSLog(@"搜索行%ld",(long)indexPath.row);
        }else{
           
            NSLog(@"正常%ld",indexPath.row);
        }
       
       
    }
    一天一章
  • 相关阅读:
    iOS项目之wifi局域网传输文件到iPhone的简单实现
    iOS项目之苹果审核被拒
    iOS项目之模拟请求数据
    nvm-window常用命令
    初探浏览器渲染原理
    node + mongodb 简单实现自己的查询接口
    快速理解_.debounce方法
    tr标签使用hover的box-shadow效果不生效
    一个简单的Node命令行程序:文件浏览
    打造丝般顺滑的 H5 翻页库(传送门)
  • 原文地址:https://www.cnblogs.com/hangman/p/5433358.html
Copyright © 2011-2022 走看看