iOS谓词做出自定义搜索效果

- (void)viewDidLoad {

    [super viewDidLoad];

//第一个数组为总库存,第二个数组用来存储搜索后的结果

    _array=[NSMutableArray array];

    _arrayb=[NSMutableArray array];

    [_array addObject:@"1df"];

    [_array addObject:@"2fbjdb"];

    [_array addObject:@"3dbfjg"];

    [_array addObject:@"nnnnn"];

    [_array addObject:@"uuuuu"];

    

    [self creatTableview];

}


//tableview展示搜索前和搜索后的结果

-(void)creatTableview{

    _TableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

    _TableView.dataSource=self;

    _TableView.delegate=self;

    [_TableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

    [self.view addSubview:_TableView];


}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



#pragma mark tableview代理事件

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 70;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 60;

}



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

{

    if ([_arrayb count]==0) {

     return [_array count];

    }else{

        return [_arrayb count];

    }

   

}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}


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

{

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    

    if ([_arrayb count]==0) {

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

    }else{

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

    }

    return cell;

    

}


//头视图里放textfield用来做搜索框(非系统自带)

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

    UIView *view=[[UIView alloc]init];

    _textF=[[UITextField alloc]init];

    _textF.placeholder=@"搜索供应商";

    _textF.delegate=self;

    _textF.backgroundColor=[UIColor whiteColor];

    [_textF addTarget : self action : @selector (didDone:) forControlEvents : UIControlEventEditingChanged ];

    _textF.frame=CGRectMake(30, 10, self.view.bounds.size.width-60, 50);

    [view addSubview:_textF];

    return view;

}


//谓词过滤,实时搜索,刷新

-(void)didDone:(UITextField *)sender

{

    [_arrayb removeAllObjects];

  NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",_textF.text];

    for  (NSString *str in _array) {

        if ([predicate evaluateWithObject:str]) {

            if ([_arrayb containsObject:str]) {

                

            }else{

            [_arrayb addObject:str];

            }

        }

    }

    if ([_arrayb count]!=0) {

     [_TableView reloadData];

    }

    if ([_textF.text isEqualToString:@""]) {

        [_arrayb removeAllObjects];

        [_TableView reloadData];

    }

}





转载于:https://my.oschina.net/applexu/blog/620130

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值