输入文本实时搜索显示

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate,UITextFieldDelegate>
{
    //原始数据
    NSArray *_fontsArray;
    //改变之后的数据
    NSArray *_array;
    UITextField *_textField;
    UITableView *_tableView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    //设置tableView代理为自己
    _tableView.dataSource = self;
    _tableView.delegate = self;

    [self.view addSubview:_tableView];

    //拿到原始数据
    _fontsArray = [UIFont familyNames];
    //拿到改变之后的数据
    _array = _fontsArray;
    //创建_textField
    _textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
   //把文本输入框加载到导航栏标题视图
    self.navigationItem.titleView = _textField;
    _textField.borderStyle = UITextBorderStyleRoundedRect;

    //方法1:给textfiled添加UIControlEventEditingChanged事件,编辑时可以实时调用textChange
    [_textField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
    //方法2:设置代理
    _textField.delegate = self;
    //方法3:添加一个文本框变化通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationChange:) name:UITextFieldTextDidChangeNotification object:nil];

}
//方法1,实现文本改变方法
- (void)textChange {
    //读取文本框内容
    NSString *text = _textField.text;

    NSString *str = [NSString stringWithFormat:@"self like [c]'*%@*'", text];
    //创建谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:str];
    //数组调用谓词方法,返回数组
    _array = [_fontsArray filteredArrayUsingPredicate:predicate];
    //重新加载数据
    [_tableView reloadData];
}
//方法2文本框字符串改变实时调用
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    //读取文本框内容
    NSString *text = [NSString stringWithFormat:@"%@%@",textField.text,string];

    NSString *str = [NSString stringWithFormat:@"self like [c]'*%@*'", text];
    //创建谓词[c]不区分大小写
    NSPredicate *predicate = [NSPredicate predicateWithFormat:str];
    //数组调用谓词方法,返回数组
    _array = [_fontsArray filteredArrayUsingPredicate:predicate];
    //重新加载数据
    [_tableView reloadData];

    return YES;
}
//方法3通知
-(void)notificationChange:(NSNotification *)notification {
    //读取文本框内容
    UITextField *textField = notification.object;

    NSString *str = [NSString stringWithFormat:@"self like [c]'*%@*'", textField.text];
    //创建谓词[c]不区分大小写 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:str];
    //数组调用谓词方法,返回数组
    _array = [_fontsArray filteredArrayUsingPredicate:predicate];
    //重新加载数据
    [_tableView reloadData];
}
#pragma mark 实现datasource
//设置组数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _array.count;
}
//创建单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

    }
    cell.textLabel.text = _array[indexPath.row];
    return cell;
}
@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值