#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