• UISearchBar


    UISearchBar  

    #import <UIKit/UIKit.h>

    @interface SearchViewController :UIViewController<UISearchBarDelegate,UISearchDisplayDelegate,UITableViewDelegate,UITableViewDataSource>

    {

        

    }

    @property(nonatomic,strong)UISearchBar* searchBar;

    @property(nonatomicstrong)UITableView* tableView;

    @property(nonatomic , strong)NSMutableArray* array1;

    @property(nonatomic , strong)NSMutableArray* array2;

    @end

    #import "SearchViewController.h"

    @interface SearchViewController ()

    @end

    @implementation SearchViewController

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil

    {

        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

        if (self) {

            // Custom initialization

        }

        return self;

    }

    - (void)viewDidLoad

    {

        [super viewDidLoad];

    // Do any additional setup after loading the view.

        

       _searchBar = [[UISearchBar allocinitWithFrame:CGRectMake(00,32040)];

        

        self.searchBar.placeholder = @"请输入所要查询的关键字";

        

        self.searchBar.showsCancelButton = YES;

        

        [self.view addSubview:_searchBar];

        

        

        UISearchDisplayController* searchDisPlay = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];

        searchDisPlay.delegate = self;

        

        searchDisPlay.searchResultsDataSource = self;

        

        searchDisPlay.searchResultsDelegate = self;

        

      

        _array1 =[[NSMutableArray allocinitWithCapacity:2];

        

        [self.array1 addObject:@"1"];

        [self.array1 addObject:@"12"];

        [self.array1 addObject:@"123"];

        [self.array1 addObject:@"1234"];

        [self.array1 addObject:@"12345"];

        [self.array1 addObject:@"54321"];

        [self.array1 addObject:@"5432"];

        [self.array1 addObject:@"543"];

        [self.array1 addObject:@"54"];

        [self.array1 addObject:@"5"];

      _array2 = [[NSMutableArray allocinitWithCapacity:2];

           

        _tableView = [[UITableView allocinitWithFrame:CGRectMake(040320,self.view.bounds.size.height - 40)];

        

        self.tableView.delegate = self;

        

        self.tableView.dataSource = self;    

        [self.view addSubview:self.tableView];

            

    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    {

        if(tableView == self.searchDisplayController.searchResultsTableView)

        {

            return self.array2.count;

        }

        

        return self.array1.count;

    }

    - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        static NSString* identifier = @"cell";

        

        UITableViewCell* cell = [tableViewdequeueReusableCellWithIdentifier:identifier];

        

        if(!cell)

        {

            cell = [[[UITableViewCell allocinitWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier] autorelease];

        

        }

        

        if(tableView == self.searchDisplayController.searchResultsTableView)

        {

            cell.textLabel.text = [self.array2 objectAtIndex:indexPath.row];

        }

        else

        {

            cell.textLabel.text = [self.array1 objectAtIndex:indexPath.row];

        }

        

        

        

        return cell;

    }

    - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption

    {

            

        return YES;

    }

    - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString

    {

        

         [self.array2 removeAllObjects];

        for(int i = 0; i < self.array1.count; i++)

        {

            if([[self.array1 objectAtIndex:i] hasPrefix:searchString])

            {

                [self.array2  addObject:[self.array1 objectAtIndex:i]];

            }

        }

        

        return YES;

    }

  • 相关阅读:
    Yougth的最大化(好题,二分查找 0 1分数规划)
    Cable master(好题,二分)
    Can you find it?(二分 二分+STL set map)
    Can you solve this equation?(二分)
    Bridging signals(二分 二分+stl dp)
    A Bug's Life
    Is It A Tree?(并查集)
    简单计算器(栈)
    Linux学习之常用压缩命令(三)
    Linux系统之常用文件搜索命令
  • 原文地址:https://www.cnblogs.com/ZGSmile/p/3491701.html
Copyright © 2020-2023  润新知