输入文本实时搜索显示
2016-05-11 19:35
399 查看
#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
相关文章推荐
- 反射机制--获取Class中的字段
- 点名器
- 第10、11周项目1:点-圆-圆柱类族的设计(3)
- Android 欢迎页面
- oracle数据迁移到db2数据库
- 对于时间管理初识--时间管理入门
- 红黑树简介
- paramiko 远程登录
- paramiko exec_command 返回 shell 脚本 exit 值
- python 的 virtualenv
- css3新的选择器
- SAP SMARTFORM 记录实际打印次数
- 关于this指向问题
- 正则表达式的简单运用
- websocket简单介绍
- 生成二维码
- bottle 轻量级框架
- python 报错
- 实验三、进程调度模拟程序
- selenium---邮件模板