美文网首页
iOS-搜索框 模糊查询 1 (UISearchControll

iOS-搜索框 模糊查询 1 (UISearchControll

作者: Mn_Su | 来源:发表于2016-09-23 13:18 被阅读0次

一、模糊查询

第一种方法:UISearchController

    1.代理协议 和 属性    相关

        代理协议:
        UITableViewDelegate,UITableViewDataSource,UISearchControllerDelegate,UISearchResultsUpdating
    
        属性:
        //tableView
        @property (strong, nonatomic)  UITableView *tableView;
        //searchController
        @property (strong, nonatomic)  UISearchController *searchController;
        //数据源
        @property (strong,nonatomic) NSMutableArray  *dataList;
        @property (strong,nonatomic) NSMutableArray  *searchList;

    2.数组初始化和创建tableview searchcontroller

        - (void)viewDidLoad {
            [super viewDidLoad];
            
            _dataList = [NSMutableArray array];
            _searchList = [NSMutableArray array];
            
            self.dataList=[NSMutableArray arrayWithCapacity:100];
            
            //产生100个“数字+三个随机字母”
            for (NSInteger i=0; i<100; i++) {
                [self.dataList addObject:[NSString stringWithFormat:@"%ld%@",(long)i,[self shuffledAlphabet]]];
            }
            
            //创建UISearchController
            self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
            //设置代理
            _searchController.delegate = self;
            _searchController.searchResultsUpdater= self;
            //设置UISearchController的显示属性,以下3个属性默认为YES
            //搜索时,背景变暗色
            _searchController.dimsBackgroundDuringPresentation = NO;
            //搜索时,背景变模糊
            _searchController.obscuresBackgroundDuringPresentation = 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);
            _searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
            _searchController.searchBar.backgroundColor = [UIColor whiteColor];
            
            /// tableview
            _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20,WIDTH ,HEIGHT-64) style:UITableViewStylePlain];
            _tableView.delegate = self;
            _tableView.dataSource = self;
            _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        //    _tableView.bounces = NO;
            _tableView.showsVerticalScrollIndicator = NO;
            [self.view addSubview:_tableView];
            _tableView.tableHeaderView = _searchController.searchBar;
    
                }
    
    3.假数据随机方法 和 相关代理

        //产生3个随机字母
        - (NSString *)shuffledAlphabet {
            
            NSMutableArray * shuffledAlphabet = [NSMutableArray arrayWithArray:@[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"]];
            
            NSString *strTest = [[NSString alloc]init];
            for (int i=0; i<3; i++) {
                int x = arc4random() % 25;
                strTest = [NSString stringWithFormat:@"%@%@",strTest,shuffledAlphabet[x]];
            }
            
            return strTest;
            
        }
        
        
        //设置区域的行数
        -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
            if (self.searchController.active) {
                return [self.searchList count];
            }else{
                return [self.dataList count];
            }
        }
        
        
        //返回单元格内容
        -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
            static NSString *flag=@"cell";
            UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag];
            if (cell==nil) {
                cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag];
            }
            if (self.searchController.active) {
                [cell.textLabel setText:self.searchList[indexPath.row]];
            }
            else{
                [cell.textLabel setText:self.dataList[indexPath.row]];
            }
            return cell;
        }

        #pragma mark - UISearchControllerDelegate代理
        
        //测试UISearchController的执行过程
        
        - (void)willPresentSearchController:(UISearchController *)searchController
        {
            NSLog(@"willPresentSearchController");
        }
        
        - (void)didPresentSearchController:(UISearchController *)searchController
        {
            NSLog(@"didPresentSearchController");
        }
        
        - (void)willDismissSearchController:(UISearchController *)searchController
        {
            NSLog(@"willDismissSearchController");
        }
        
        - (void)didDismissSearchController:(UISearchController *)searchController
        {
            NSLog(@"didDismissSearchController");
        }
        
        - (void)presentSearchController:(UISearchController *)searchController
        {
            NSLog(@"presentSearchController");
        }
        
        
        -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
            
            // 修改UISearchBar右侧的取消按钮文字颜色及背景图片
            searchController.searchBar.showsCancelButton = YES;
            for (id searchbuttons in [[searchController.searchBar subviews][0] subviews]) //只需在此处修改即可
                
                if ([searchbuttons isKindOfClass:[UIButton class]]) {
                    UIButton *cancelButton = (UIButton*)searchbuttons;
                    // 修改文字颜色
                    [cancelButton setTitle:@"返回"forState:UIControlStateNormal];
                    [cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                }
        
            
            NSLog(@"updateSearchResultsForSearchController");
            NSString *searchString = [self.searchController.searchBar text];
            NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
            if (self.searchList!= nil) {
                [self.searchList removeAllObjects];
            }
            //过滤数据
            self.searchList= [NSMutableArray arrayWithArray:[_dataList filteredArrayUsingPredicate:preicate]];
            //刷新表格
            [self.tableView reloadData];
        }

第二种方法:UISearchBar

        1.代理和属性

            UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate

            //tableView
            @property (strong, nonatomic)  UITableView *tableView;
            //searchBar
            @property (nonatomic , strong) UISearchBar *search;
            @property (nonatomic , strong) UIButton *searchCancleBtn;
            
            //数据源
            @property (strong,nonatomic) NSMutableArray  *dataList;
            @property (strong,nonatomic) NSMutableArray  *searchList;

        2.初始化

            /** 搜索框背景图片的封装方法 */
            - (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size{
                CGRect recte = CGRectMake(0, 0, size.width, size.height);
                UIGraphicsBeginImageContext(recte.size);
                CGContextRef context = UIGraphicsGetCurrentContext();
                
                CGContextSetFillColorWithColor(context, [color CGColor]);
                CGContextFillRect(context, recte);
                
                UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
                
                return image;
            }

            - (void)viewDidLoad {
                [super viewDidLoad];
        
                _dataList = [NSMutableArray array];
                _searchList = [NSMutableArray array];
                
                self.dataList=[NSMutableArray arrayWithCapacity:100];
                
                //产生100个“数字+三个随机字母”
                for (NSInteger i=0; i<100 xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed i=0; xss=removed xss=removed> 0) {
                    return [self.searchList count];
                }else{
                    return [self.dataList count];
                }
            }
            
            
            //返回单元格内容
            -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
                static NSString *flag=@"cell";
                UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag];
                if (cell==nil) {
                    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag];
                    cell.backgroundColor = [UIColor yellowColor];
                }
                if (self.inputString && self.inputString.length > 0) {
                    [cell.textLabel setText:self.searchList[indexPath.row]];
                }
                else{
                    [cell.textLabel setText:self.dataList[indexPath.row]];
                }
                return cell;
            }

二、UISearchBar

第一种方式

        1.创建UISearchBar
    
            UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 72, WIDTH, HEIGHT/16+10)];
            bgView.backgroundColor = WHITECOLOR;
            [self.view addSubview:bgView];
            
            UISearchBar *search = [[UISearchBar alloc]initWithFrame:CGRectMake(5, 5, WIDTH-10, HEIGHT/16)];
            search.placeholder =  @"搜索朋友";
            search.backgroundColor = REDCOLOR;
            search.backgroundImage = [self imageWithColor:WHITECOLOR size:search.bounds.size];
            search.layer.borderColor = CAOGREENCOLOR.CGColor;
            search.layer.borderWidth = 2;
            search.layer.cornerRadius = 3;
            search.clipsToBounds = YES;
            [bgView addSubview:search];
        
        2.搜索框表象封装方法
            
        /** 搜索框背景图片的封装方法 */
        - (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size{
            CGRect recte = CGRectMake(0, 0, size.width, size.height);
            UIGraphicsBeginImageContext(recte.size);
            CGContextRef context = UIGraphicsGetCurrentContext();
        
            CGContextSetFillColorWithColor(context, [color CGColor]);
            CGContextFillRect(context, recte);
            
            UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            
            return image;
        }

        - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
            self.inputString = searchText;
            NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchText];
            if (self.searchList!= nil) {
                [self.searchList removeAllObjects];
            }
            //过滤数据
            self.searchList = [NSMutableArray arrayWithArray:[_dataList filteredArrayUsingPredicate:preicate]];
            
            //刷新表格
            [self.tableView reloadData];
        
        }

第二种方式

        //搜索框
        self.search = [[UISearchBar alloc]initWithFrame:CGRectMake(100, 30, WIDTH-100-100, 30)];
      self.search.placeholder =  @"请搜索输入";
      self.search.searchBarStyle = UISearchBarStyleMinimal;
      self.search.delegate = self;
      self.navigationItem.titleView = self.search;
      //    [search setBackgroundImage:[[UIImage alloc] init]];
      //    [search setImage:[UIImage imageNamed:@"error.png"] forSearchBarIcon:UISearchBarIconClear     state:UIControlStateNormal];

相关文章

  • iOS-搜索框 模糊查询 1 (UISearchControll

    一、模糊查询 第一种方法:UISearchController 第二种方法:UISearchBar 二、UISea...

  • 搜索框模糊查询

    前端html代码 前端js代码 前端css代码 后端php返回json

  • 移动端H5问题记录

    1、iOS-搜索框无法输入解决方案:给 input框添加 style="-webkit-user-select:a...

  • docker搭载elasksearch

    1. 需求分析 当用户在搜索框输入关键字后,我们要为用户提供相关的搜索结果。 这种需求依赖数据库的模糊查询like...

  • web中的ElasticSearch使用

    1. 需求分析 当用户在搜索框输入关键字后,我们要为用户提供相关的搜索结果。 这种需求依赖数据库的模糊查询like...

  • 关于搜索框的交互

    首先输入框搜索流程 搜索功能从大的方向分为精确搜索和模糊搜索。精确搜索针对用户对搜索目标十分精确,通常是物流查询订...

  • iOS-模糊查询

    前言: 为了巩固FMDB,就来找个简单的Demo学习一下。不好找工作啊,就学习吧,没应聘的消遣吧。 简单介绍:模糊...

  • iOS高德地图开发【二】搜索与导航

    喜大普奔,终于,整理了搜索和导航。效果图先奉上: 1.依据关键词进行搜索 监听文本框编辑的变化,实现关键字模糊查询...

  • 搜索框实现模糊查询方法 - js

    日常开发中经常会遇到搜索框查询的例子,需要对数组中的元素进行检索并查询出所需的结果,通过如下方式简单实现了一个小的...

  • 查询功能点测试方法总结[转载]

    一、测试方法 查询类型包含单个查询、组合查询、输入框输入查询、时间控件查询四种场景: 1、功能实现 (1)支持模糊...

网友评论

      本文标题:iOS-搜索框 模糊查询 1 (UISearchControll

      本文链接:https://www.haomeiwen.com/subject/veutyttx.html