输入文本实时搜索显示
来源:互联网 发布:mac里的照片导入ipad 编辑:程序博客网 时间:2024/05/21 09:53
#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
0 0
- 输入文本实时搜索显示
- textArea实时显示文本输入框输入的字数
- AJAX实时读取输入文本
- 常用的点击搜索框显示下拉框并实时刷新用户输入的内容
- 实时显示输入文本框字数
- ThinkPHP3.2.3---ajaxreturn的应用:一个在搜索框中输入,实时动态显示模糊搜索结果
- AngularJS—实时获取输入的文本
- textarea文本域输入字数实时控制
- tkinter文本显示用户输入
- 在EditText输入文本并实时改变文本的数量
- PyQt4实时显示文本内容GUI
- Android搜索实时显示功能实现
- Android 仿微博实时显示输入文字长度
- jQuery—实时显示输入框字数
- 实时显示textarea输入多少字
- EditText右下角实时显示输入字数
- 文本输入框实时统计字数,并防止重复提交
- 显示行号的文本输入框
- paramiko 远程登录
- 第五次c++实验
- 红黑树简介
- paramiko exec_command 返回 shell 脚本 exit 值
- Spark-SparkSQL深入学习系列九(转自OopsOutOfMemory)
- 输入文本实时搜索显示
- Android 欢迎页面
- 服务器挂了可能引发的问题
- 第10、11周项目1:点-圆-圆柱类族的设计(3)
- Spark-SparkSQL深入学习系列八(转自OopsOutOfMemory)
- python 的 virtualenv
- 结构体版本兼容问题
- JavaScript动态创建Input输入框动态获取值
- [bzoj4553][TJOI&HEOI2016]序列