搜索框联想功能实现

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];//生成一个五位数的字符串
        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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 树丰老师最近组织了秋季写书计划,今天下午泡在图书馆一下午的时间寻找写作的一些方法,为了后期写作做引导,这或许是最为...
    锦潇阅读 2,999评论 0 2
  • 今天分享来自《沉思录》这本书的十点感悟: 001 既能够很好的承受,又能够时刻保持清醒的人,才是一个灵魂完善且不可...
    饼姑娘阅读 1,768评论 0 0
  • 首先,你需要注册一个帐号。 想要上传的话首先要登录[root@docker ~]# docker loginUs...
    如来自然阅读 4,284评论 0 1
  • 此篇大量图片,解说甚少 入门风景如下, 各种热带高大的树木 睡莲前的蓝天白云,太适合当自拍的背景板了。 跳舞草,我...
    橙子林林阅读 3,916评论 0 0
  • 做了个梦,梦见这句话,自信的人最自然!
    阳茗阅读 1,703评论 0 1