UISearchController是iOS8出的
如果要适配iphone4的朋友,就别使用UISearchController,4不能升级到iOS8。
直接上代码
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating>
@property (nonatomic,strong) UISearchController *searchController;
@property (nonatomic,strong)UITableView *tableView;
//数据源
@property (strong,nonatomic)NSMutableArray *dataList;
//筛选后的数据
@property (strong,nonatomic)NSMutableArray *searchList;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 创建tableView
self.tableView = [[UITableViewalloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height)style:UITableViewStylePlain];
self.tableView.dataSource =self;
self.tableView.delegate =self;
[self.viewaddSubview:self.tableView];
// 创建UISearchController
_searchController = [[UISearchControlleralloc] initWithSearchResultsController:nil];
_searchController.searchResultsUpdater =self;
_searchController.dimsBackgroundDuringPresentation =NO;
_searchController.hidesNavigationBarDuringPresentation =NO;
_searchController.searchBar.frame =CGRectMake(self.searchController.searchBar.frame.origin.x,self.searchController.searchBar.frame.origin.y,self.searchController.searchBar.frame.size.width, 44.0);
self.tableView.tableHeaderView =self.searchController.searchBar;
self.dataList=[NSMutableArrayarrayWithCapacity:100];
for (NSInteger i=0; i<100; i++) {
[self.dataListaddObject:[NSStringstringWithFormat:@"%ld-俊彩飞扬",(long)i]];
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
// self.searchController.active 当前的状态
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (self.searchController.active) {
return [self.searchListcount];
}else{
return [self.dataListcount];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *flag=@"cellFlag";
UITableViewCell *cell=[tableViewdequeueReusableCellWithIdentifier:flag];
if (cell==nil) {
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:flag];
}
if (self.searchController.active) {
[cell.textLabel setText:self.searchList[indexPath.row]];
}
else{
[cell.textLabel setText:self.dataList[indexPath.row]];
}
return cell;
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
NSLog(@"搜索Begin");
return YES;
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
NSLog(@"搜索End");
return YES;
}
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString *searchString = [self.searchController.searchBartext];
// 筛选谓词
NSPredicate *preicate = [NSPredicatepredicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
if (self.searchList!=nil) {
[self.searchListremoveAllObjects];
}
//过滤数据
self.searchList= [NSMutableArrayarrayWithArray:[_dataListfilteredArrayUsingPredicate:preicate]];
//刷新表格
[self.tableViewreloadData];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end