UISearchController和谓词

UISearchController 搜索框

通过searchBar设置frame

 self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, self.searchController.searchBar.frame.size.height);

属性设置

   // 遮挡板的属性
    self.searchController.dimsBackgroundDuringPresentation = NO;
    // 
    self.searchController.hidesBottomBarWhenPushed = NO;
    // 指定代理人
    self.searchController.searchResultsUpdater = self;
    // 让tableView的头是search框
    self.tableView.tableHeaderView = self.searchController.searchBar;

引入searchController之后,tableView的内容也要相应的变动,判断是tableView显示的,还是搜索出来的,所以用active这个属性来判断,如果是active返回搜索的数组

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // 如果和数组内容相符就弹出内容,如果不符就还是原来的
    if (self.searchController.active) {
        return self.searchList.count;
    } else {
        return  self.arr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
    }
    if (self.searchController.active) {
        // 根据搜索框来指定cell的显示内容
        [cell.textLabel setText:self.searchList[indexPath.row]];
    } else{
        [cell.textLabel setText:self.arr[indexPath.row]];
    }
    return  cell;
<h1>}</h1>


签协议<UISearchResultsUpdating>,实现方法,searchController的searchBar一旦发生变化,就会调用这个方法,在其中我们需要用谓词来过滤条件

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    NSString *searchString = [self.searchController.searchBar text];
    // 谓词(群空间有)  谓词(关键词高亮)
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
    if (self.searchList != nil) {
        [self.searchList removeAllObjects];
    }
    // 过滤数据,筛选内容
    self.searchList = [NSMutableArray arrayWithArray:[self.arr filteredArrayUsingPredicate:predicate]];
    [self.tableView reloadData];

}

谓词:针对数组类型的查询操作,通过谓词可以过滤数组中元素,从而找到我们需要的元素,节省代码

1.创建一个需要遍历的数组

  Student *stu1 = [Student studentWithName:@"张JJ" age:22 sex:@"男"];
    Student *stu2 = [Student studentWithName:@"兆宇" age:22 sex:@"男"];
    Student *stu3 = [Student studentWithName:@"大陌" age:20 sex:@"男"];
    Student *stu4 = [Student studentWithName:@"程锦乐" age:22 sex:@"女"];
    Student *stu5 = [Student studentWithName:@"马云" age:20 sex:@"豪"];
    Student *stu6 = [Student studentWithName:@"郡主" age:22 sex:@"女"];
    
    NSArray *arr = [NSArray arrayWithObjects:stu1,stu2,stu3,stu4,stu5,stu6, nil];

2.找到年龄小于25 [NSPredicate predicateWithFormat:@"age< %ld", 22]; 过滤:filteredArrayUsingPredicate

  //年龄小于25的
    
    //定义谓词对象.谓词对象包含过滤条件
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"age<%d",22];
    //过滤出符合条件的对象放到一个新的数组中
    NSArray *arr1= [arr filteredArrayUsingPredicate:pre];
    //打印符合条件的对象
    for (NSInteger i = 0; i < arr1.count; i++) {
        NSLog(@"%@",[arr1[i] description]);
    }


3.找到年龄为22的男性

 //查询年龄为22的男性
    //定义谓词对象和过滤条件
    NSPredicate *pre1 = [NSPredicate predicateWithFormat:@"age = %d && sex = %@",22,@"男"];
    //添加到新数组
    NSArray *arr2 = [arr filteredArrayUsingPredicate:pre1];
    //打印符合条件的对象
    for (NSInteger i = 0; i < arr2.count; i++) {
        NSLog(@"%@",[arr2[i] description]);
    }

4.包含:找到性别是女和豪的

  //包含....找到性别是女和豪的
    NSPredicate *pre2 = [NSPredicate predicateWithFormat:@"self.sex IN {'女','豪'}"];
    NSArray *arr3 = [arr filteredArrayUsingPredicate:pre2];
    for (NSInteger i = 0; i < arr3.count; i++) {
        NSLog(@"%@",[arr3[i] description]);
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值