zoukankan      html  css  js  c++  java
  • UISearchController

    #import "SearchViewController.h"

    @interface SearchViewController ()< UISearchResultsUpdating,  UITableViewDelegate, UITableViewDataSource>//UISearchController需要接受协议

    @property (nonatomic, retain)  UISearchController *searchController;

    @property (nonatomic, retain)  UITableView *tableView;

    //保存tableView的数据

    @property (nonatomic, retain)  NSMutableArray *dataArray;

    //保存searchController搜索结果的数据

    @property (nonatomic, retain)  NSMutableArray *searchArray;

    @end

    @implementation SearchViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        self.navigationItem.title = @"列表";

        //初始化tableView

        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];    

        _tableView.delegate = self;

        _tableView.dataSource = self;

        [self.view addSubview:_tableView];

        

        //searchResultsController参数设置为nil,能在相同的视图中显示搜索结果

        _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];

        self.searchController.searchBar.frame = CGRectMake(0, 0, 375, 40);

        //设置代理 搜索数据更新

        self.searchController.searchResultsUpdater = self;

        //遮盖层

        self.searchController.dimsBackgroundDuringPresentation = YES;

        //当点击输入框的时候导航视图控制器的显隐问题

        self.searchController.hidesNavigationBarDuringPresentation = YES;

        //弹出键盘的样式

        self.searchController.searchBar.keyboardType = UIKeyboardTypeDefault;

        //输入框的占位符

        self.searchController.searchBar.placeholder = @"搜索";

        //搜多框颜色

        self.searchController.searchBar.barTintColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:0.3];

        //光标的颜色

        self.searchController.searchBar.tintColor = [UIColor grayColor];

        //书签button

    //    self.searchController.searchBar.showsBookmarkButton = YES;

    //    self.searchController.searchBar.showsCancelButton = NO;//默认是no,当点击的时候出现

        //tableView指定 头视图!!!!!!

        self.tableView.tableHeaderView = self.searchController.searchBar;

        

        [self createContant];

    }

    //给声明的数组存值

    - (void)createContant {

        _dataArray = [[NSMutableArray alloc] initWithCapacity:0];

        for (int i = 0; i < 50; i ++) {

            int  str = arc4random() % (1000 - 10 + 1) + 10;

            NSString *str1 = [NSString stringWithFormat:@"%d", str];

            [_dataArray addObject:str1];

          }

    }

    #pragma mark-------SearchViewController的协议方法-------------

    //必须实现的方法

    - (void)updateSearchResultsForSearchController:(UISearchController *)searchController {

        //Cancel改为中文

        searchController.searchBar.showsCancelButton = YES;//显示“取消”按钮。这个不设置会显示默认的cancel样式

        UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];

        UIView *topView = self.searchController.searchBar.subviews[0];

        NSLog(@"%@", topView);

        for (UIView *subView in topView.subviews) {

            //判断 查找button的类 即使想要得到的 视图

            if ([subView isKindOfClass:[UIButton class]]) {

                cancelButton = (UIButton*)subView;//需要加强转,把cancel按钮赋值给声明的button,再进行操作,原有的属性不变

                [cancelButton setTitle:@"取消" forState:UIControlStateNormal];//设置button的标题

                cancelButton.tintColor = [UIColor grayColor];//设置button的颜色

            }

        }

        //获取在输入框输入的值

        NSString *searchString = [self.searchController.searchBar text];

        //注意:1,使用谓词表示逻辑条件,用于描述对象持久存储和内存筛选的对象,从这个可变数组中查找其中某个字符串带有你在输入框输入的内容的内容;2,self表示要查询集合中对象,contain[c]表示包含字符串,其中c是不区分大小写

        //创建谓词

        NSPredicate *preicate = [NSPredicate predicateWithFormat:@"self contains[cd] %@", searchString];

        //删除原有存数的数值

        if (self.searchArray != nil) {

            [self.searchArray removeAllObjects];

        }

        //根据提供的数据数组筛选出符合条件的值,在搜索结果表视图内部显示

        self.searchArray = [NSMutableArray arrayWithArray:[self.dataArray filteredArrayUsingPredicate:preicate]];

        //更新表视图的数据

        [self.tableView reloadData];

    }

    #pragma mark ---------tableView的协议方法------------

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

        //active(活动) 当用户点击搜索栏,搜索显示控制器自动显示搜索界面

        if (self.searchController.active) {

            return _searchArray.count;//搜索的数组,设置tableView的cell的个数

        }else{

            return _dataArray.count;//得到返回tableView的cell的个数

        }

    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

        static NSString *reuse = @"reuse";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];

        if (cell == nil) {

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];

        }

       //设置cell时要加判断,来确定是那类cell

        if (self.searchController.active) {//action是个BOOL值

            cell.textLabel.text = _searchArray[indexPath.row];

        }else{

            cell.textLabel.text = _dataArray[indexPath.row];

        }

        return cell;

    }

  • 相关阅读:
    List 集合的常用方法总结
    springboot 整合 web 项目找不到 jsp 文件
    Web 安全
    微服务开放平台接口设计
    SpringCloud Hystrix 参数
    SpringCloud Eureka 配置
    ELK 日志收集系统
    网盘搜索引擎原理
    jsPlumb.jsAPI阅读笔记(官方文档翻译)
    ionic获取ios唯一设备id的解决方案
  • 原文地址:https://www.cnblogs.com/jiurong001/p/5197733.html
Copyright © 2011-2022 走看看