zoukankan      html  css  js  c++  java
  • iOS-UISearchBar和UISearchController(参考网友来练习一下)

    #import "ViewController.h"
    #import "TestCell.h"
    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchResultsUpdating>
    {
        NSMutableArray *dataArray;
        NSMutableArray *searchArray;
        UISearchController *mySearchController;
       
    }
    @property (weak, nonatomic) IBOutlet UITableView *myTableView;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        mySearchController=[[UISearchController alloc]initWithSearchResultsController:nil];
        mySearchController.searchResultsUpdater=self;
        mySearchController.dimsBackgroundDuringPresentation=NO;
        mySearchController.hidesNavigationBarDuringPresentation=NO;
        mySearchController.searchBar.frame=CGRectMake(mySearchController.searchBar.frame.origin.x, mySearchController.searchBar.frame.origin.y, mySearchController.searchBar.frame.size.width, 44.0 );
        self.myTableView.tableHeaderView=mySearchController.searchBar;
        
        dataArray=[NSMutableArray array];
        for(NSInteger i=0;i<100;i++)
        {
            [dataArray addObject:[NSString stringWithFormat:@"%ld-test",(long)i]];
        }
        
    }
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if (mySearchController.active)
        {
            return searchArray.count;
        }
        else
        {
            return dataArray.count;
        }
    
    }
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 44;
    }
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        TestCell *testCell=[tableView dequeueReusableCellWithIdentifier:@"TestCell" forIndexPath:indexPath];
        if (mySearchController.active) {
            testCell.testLabel.text=searchArray[indexPath.row];
        }
        else
        {
            testCell.testLabel.text=dataArray[indexPath.row];
        }
        
        return testCell;
    }
    -(void)updateSearchResultsForSearchController:(UISearchController *)searchController
    {
        NSString *searchString=mySearchController.searchBar.text;
        NSPredicate *predicate=[NSPredicate predicateWithFormat:@"self contains[c]%@",searchString];
        if (searchArray.count)
        {
            [searchArray removeAllObjects];
        }
        searchArray=[NSMutableArray arrayWithArray:[dataArray filteredArrayUsingPredicate:predicate]];
        [_myTableView reloadData];
        
    }
    @end
  • 相关阅读:
    shell 生成指定范围随机数与随机字符串
    学习C#和SQL的书籍
    WEB安全:SQL注入
    Android TP(三)【转】
    Android 使用MediaRecorder录音调用stop()方法的时候报错【转】
    我的Android进阶之旅------>Android中MediaRecorder.stop()报错 java.lang.RuntimeException: stop failed.【转】
    Android App调用MediaRecorder实现录音功能的实例【转】
    MODULE_DEVICE_TABLE【转】
    MODULE_DEVICE_TABLE的理解【转】
    android kl 文件的作用【转】
  • 原文地址:https://www.cnblogs.com/thbbsky/p/4276472.html
Copyright © 2011-2022 走看看