博客> iOS-搜索框 模糊查询 1 (UISearchController 和 UISearchBar)
iOS-搜索框 模糊查询 1 (UISearchController 和 UISearchBar)
2017-11-25 10:45 评论:0 阅读:326 OS_JJ
ios 搜索框 模糊查询 (UISearchController UISearchBar)

一、模糊查询

第一种方法: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 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 xss=removed xss=removed i=0; xss=removed xss=removed flag=@ cell=[tableView xss=removed cell=[[UITableViewCell xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed self.dataList=[NSMutableArray i=0; 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];
收藏
1
sina weixin mail 回到顶部