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);
        }
       
       
    }
    一天一章
  • 相关阅读:
    *Binary Tree Inorder Traversal
    First Unique Character in a String
    回溯法模板
    *Linked List Random Node
    *Odd Even Linked List
    *Maximum Product of Word Lengths
    Flatten 2D Vector
    Docker (dockerfile)
    Docker数据卷
    Docker入门
  • 原文地址:https://www.cnblogs.com/hangman/p/5433358.html
Copyright © 2011-2022 走看看