IOS-模糊搜索UISearchBar+UISearchDisplayController

#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 = [ super initWithNibName :nibNameOrNil bundle :nibBundleOrNil];

if ( self ) {

// Custom initialization

}

return self ;

}

- ( 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 ;

}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容