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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容