zoukankan      html  css  js  c++  java
  • 模糊搜索UISearchBar

    #import "Search_ViewController.h"

    @interface Search_ViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchDisplayDelegate,UISearchBarDelegate>

    @property(nonatomic,strong)NSArray*dataArr;//数据源

    @property(nonatomic,strong)NSArray*resultsArr;//搜索结果

    @property(nonatomic,strong)UISearchBar*search;

    @property(nonatomic,strong)UISearchDisplayController* searchPlay;

    @property(nonatomic,strong)UITableView*aTableView;

    @end

    @implementation Search_ViewController

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        self.title=@"搜索";

        _dataArr= [[NSArray alloc]initWithObjects:@"aaa",@"abc",@"aqq",@"bdc",@"gcd",@"mnb",@"zzz",nil];

        [self createView];

        // Do any additional setup after loading the view.

    }

    - (void) createView

    {

        _aTableView= [[UITableView alloc]initWithFrame:CGRectMake(0,0,320,480)];

        _aTableView.delegate=self;

        _aTableView.dataSource=self;

        [self.view addSubview:_aTableView];

    }

    #pragma mark -

    #pragma mark UITableViewDelegate

    -(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section

    {

        _search= [[UISearchBar alloc]initWithFrame:CGRectMake(0,0,320,40)];

        _search.backgroundColor= [UIColor redColor];

        _search.delegate=self;

        _search.showsCancelButton=YES;

        _search.keyboardType=UIKeyboardTypeDefault;

        _searchPlay= [[UISearchDisplayController alloc]initWithSearchBar:self.search contentsController:self];

        _searchPlay.delegate=self;

        _searchPlay.searchResultsDataSource=self;

        _searchPlay.searchResultsDelegate=self;

        _searchPlay.active=NO;

        return _searchPlay.searchBar;

    }

    - (BOOL)searchBarShouldEndEditing:(UISearchBar*)searchBar

    {

        NSLog(@"取消");

        return YES;

    }

    - (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section

    {

        return  tableView ==self.searchPlay.searchResultsTableView?0:40;

    }

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

        NSInteger row =0;

        if([tableView isEqual:self.searchPlay.searchResultsTableView]) {

            row = [self.resultsArr count];

        }else{

            row = [self.dataArr count];

        }

        return row;

    }

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

    {

        static NSString *CellIdentifier =@"Cell";

        UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if(!cell) {

            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        }

        if([tableView isEqual:self.searchPlay.searchResultsTableView]) {

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

        }else{

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

        }

        return cell;

    }

    #pragma mark -

    #pragma mark UISearchDisplayControllerDelegate

    //text是输入的文本,scope是搜索范围

    - (void) searchText:(NSString*)text andWithScope:(NSString*)scope

    {

        //CONTAINS是字符串比较操作符,

        NSPredicate*result = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@",text];

        self.resultsArr= [self.dataArr filteredArrayUsingPredicate:result];

    }

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

    {

        // searchString是输入的文本

        //返回结果数据读取搜索范围在选择范围

        NSArray*searScope = [self.searchPlay.searchBar scopeButtonTitles];//数组范围

        [self searchText:searchString andWithScope:[searScope objectAtIndex:[self.searchPlay.searchBar selectedScopeButtonIndex]]];

        return YES;

    }

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

    {

        NSString*inputText =self.searchPlay.searchBar.text;//搜索输入的文本

        NSArray*searScope = [self.searchPlay.searchBar scopeButtonTitles];//索索范围

        [self searchText:inputText andWithScope:[searScope objectAtIndex:searchOption]];

        return YES;

    }

  • 相关阅读:
    软件概要设计
    项目文件-搭建工程
    select标签中设置只读几种解决方案
    PHP ob缓冲区函数的使用
    laravel笔记
    ubuntu系统更新命令
    RBAC权限控制系统
    laravel 数据库获取值的常用方法
    php中获取数据 php://input、$_POST与$GLOBALS['HTTP_RAW_POST_DATA']三者的区别
    php 文件上传 $_FILES 错误码
  • 原文地址:https://www.cnblogs.com/lrr0618/p/5177548.html
Copyright © 2011-2022 走看看