输入文本实时搜索显示

来源:互联网 发布: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
原创粉丝点击