#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
网友评论