iOS模糊搜索

#import"RootViewController.h"

@interfaceRootViewController()<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

@implementationRootViewController

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

{

self= [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];

if(self) {

// Custom initialization

}

returnself;

}

- (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(@"取消");

returnYES;

}

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

{

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

}

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

NSIntegerrow =0;

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

row = [self.resultsArrcount];

}else{

row = [self.dataArrcount];

}

returnrow;

}

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

{

staticNSString*CellIdentifier =@"Cell";

UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier: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];

}

returncell;

}

#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:searchStringandWithScope:[searScopeobjectAtIndex:[self.searchPlay.searchBarselectedScopeButtonIndex]]];

returnYES;

}

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

{

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

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

[selfsearchText:inputTextandWithScope:[searScopeobjectAtIndex:searchOption]];

returnYES;

}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值