IOS-模糊搜索UISearchBar+UISearchDisplayController

不废话上代码 想学的跟着写一遍 注释很全了。

只是实现了简单 搜索,其他都没有做处理


//

//  RootViewController.m

//  SearchTable

//

//  Created by ALei on 14-8-22.

//  Copyright (c) 2014 MinjieShou. All rights reserved.

//


#import "RootViewController.h"


@interface RootViewController ()<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 RootViewController


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

{

   self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];

   if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

   self.title =@"搜索";

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

    

    [selfcreateView];

    

    // Do any additional setup after loading the view.

}


- (void) createView

{

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

    _aTableView.delegate =self;

    _aTableView.dataSource =self;

    [self.viewaddSubview:_aTableView];

}


#pragma mark -

#pragma mark UITableViewDelegate


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

{

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

    _search.backgroundColor = [UIColorredColor];

    _search.delegate =self;

    _search.showsCancelButton =YES;

    _search.keyboardType =UIKeyboardTypeDefault;


    _searchPlay = [[UISearchDisplayControlleralloc] initWithSearchBar:self.searchcontentsController: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 ([tableViewisEqual:self.searchPlay.searchResultsTableView]) {

        row = [self.resultsArrcount];

    }else{

        row = [self.dataArrcount];

    }

   return row;

}

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

{

   static NSString *CellIdentifier =@"Cell";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   if (!cell) {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];

    }

    

    if ([tableViewisEqual:self.searchPlay.searchResultsTableView]) {

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

    }else{

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

    }

   return cell;

}





#pragma mark - 

#pragma mark UISearchDisplayControllerDelegate


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

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

{

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

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

    self.resultsArr = [self.dataArrfilteredArrayUsingPredicate:result];

}


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

{

    // searchString 是输入的文本

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

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

    [selfsearchText:searchString andWithScope:[searScopeobjectAtIndex:[self.searchPlay.searchBarselectedScopeButtonIndex]]];

     return YES;

}


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

{

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

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

    [selfsearchText:inputText andWithScope:[searScope objectAtIndex:searchOption]];

    return YES;

}




@end



IOS-模糊搜索UISearchBar+UISearchDisplayController

上一篇:JS eval()函数


下一篇:JS技巧