搜索框联想功能实现

【搜索框联想功能实现】http://www.tuicool.com/articles/vQ7Z3qu

#import "GHSearchController.h"@interface GHSearchController () @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]; //生成一个五位数的字符串 for (int x=0; x<10; data[x++] = (char)('A' + (arc4random_uniform(26)))); NSString *string = [[NSString alloc] initWithBytes:data length:5 encoding:NSUTF8StringEncoding]; //随机给字符串赋值 [_exampleArr addObject:string]; } // 随机生成200个五位数的字符串 }- (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; //搜索结果 }else { return self.exampleArr.count; //原始数据 } }- (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]; } //过滤数据 self.searchArr = [NSMutableArray arrayWithArray:[_exampleArr filteredArrayUsingPredicate:preicate]]; //刷新表格 [self.demoTableView reloadData]; } @end

    推荐阅读