http://www.tuicool.com/articles/vQ7Z3qu
#import "GHSearchController.h"
@interface GHSearchController () <UISearchResultsUpdating, UISearchControllerDelegate, UITableViewDataSource>
@property (nonatomic, strong) UISearchController *searchVC;
@property (nonatomic ,strong)UITableView *demoTableView;
@property (nonatomic ,strong) NSMutableArray *exampleArr;
@property (nonatomic ,strong)NSMutableArray *searchArr;
@end
@implementation GHSearchController
#pragma mark - 懶加載
- (UISearchController *)searchVC
{
if (!_searchVC) {
_searchVC = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchVC.searchResultsUpdater = self;
_searchVC.dimsBackgroundDuringPresentation = NO;
_searchVC.hidesNavigationBarDuringPresentation = NO;
_searchVC.searchBar.frame = CGRectMake(self.searchVC.searchBar.frame.origin.x, self.searchVC.searchBar.frame.origin.y, self.searchVC.searchBar.frame.size.width, 44.0);
self.demoTableView.tableHeaderView = self.searchVC.searchBar;
}
return _searchVC;
}
- (NSMutableArray *)exampleArr
{
if (!_exampleArr) {
}
return _exampleArr;
}
- (UITableView *)demoTableView
{
if (!_demoTableView) {
_demoTableView = [[UITableView alloc] init];
_demoTableView.dataSource = self;
}
return _demoTableView;
}
#pragma mark - view
- (void)viewDidLoad
{
[super viewDidLoad];
self.view = self.demoTableView;
_exampleArr = [NSMutableArray arrayWithCapacity:200];
CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
self.demoTableView.frame = CGRectMake(0, rectStatus.size.height, self.view.frame.size.width, self.view.frame.size.height - rectStatus.size.height);
// self.searchBar.frame = CGRectMake(0, rectStatus.size.height, self.view.frame.size.width, 50.0);
for (int i = 0; i < 200; i ++) {
int NUMBER_OF_CHARS = 5;
char data[NUMBER_OF_CHARS];//生成一個五位數(shù)的字符串
for (int x=0;x<10;data[x++] = (char)('A' + (arc4random_uniform(26))));
NSString *string = [[NSString alloc] initWithBytes:data length:5 encoding:NSUTF8StringEncoding];//隨機(jī)給字符串賦值
[_exampleArr addObject:string];
} // 隨機(jī)生成200個五位數(shù)的字符串
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath//cell
{
static NSString *identify = @"cellIdentify";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
}
if (!self.searchVC.active) {
cell.textLabel.text = [NSString stringWithFormat:@"%@",_exampleArr[indexPath.row]];
}else
{
cell.textLabel.text = [NSString stringWithFormat:@"%@",_searchArr[indexPath.row]];
}
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.searchVC.active) {
return self.searchArr.count;//搜索結(jié)果
}else
{
return self.exampleArr.count;//原始數(shù)據(jù)
}
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchString = [self.searchVC.searchBar text];
NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
if (self.searchArr!= nil) {
[self.searchArr removeAllObjects];
}
//過濾數(shù)據(jù)
self.searchArr = [NSMutableArray arrayWithArray:[_exampleArr filteredArrayUsingPredicate:preicate]];
//刷新表格
[self.demoTableView reloadData];
}
@end