电话本的检索功能

设置带有导航栏的根视图控制器

RootViewController.h

@interface RootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
{
    UITableView *_tableView;
}

@property(nonatomic, retain)NSArray *data;  //存放原本的数据
@property(nonatomic, retain)NSArray *filterData;    //用于存放删选后的数据


RootViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //创建输入框
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    textField.delegate = self;
    textField.borderStyle = UITextBorderStyleRoundedRect;
    //关闭大写
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.navigationItem.titleView = textField;
    [textField release];
    
    //创建表视图
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    //取得数据
    /*
     [@"1",@"2"]
     */
    _data = [[UIFont familyNames] retain];
    _filterData = [_data retain];
    
    //注册一个通知
    //输入框里面的内容发生改变的时候发送通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeAction:) name:UITextFieldTextDidChangeNotification object:nil];
    
}

- (void)changeAction:(NSNotification *)notificiation {

   //输入框输入的内容
    NSLog(@"%@",notificiation.object);
    UITextField *textField = notificiation.object;
    

    //定义谓词
    //[c]标示不区分大小写
    NSString *t = [NSString stringWithFormat:@"self like [c]'*%@*'",textField.text];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:t];
    //过滤元素
    self.filterData = [_data filteredArrayUsingPredicate:predicate];

    //刷新视图
    [_tableView reloadData];
    
}

#pragma mark - UITableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _filterData.count;
}

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

    static NSString *iden = @"cell110";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden] autorelease];
    }
    
    cell.textLabel.text = _filterData[indexPath.row];
    
    return cell;
    
}

#pragma mark - UITextField delegate
//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//
//    /*
//     textField.text里面存放是用户以前输入的内容
//     string:标示用户刚刚输入的字符
//     */
//    
//    NSLog(@"textField:%@",textField.text);
//    NSLog(@"string:%@",string);
//    
//    //输入框输入的内容
//    NSString *text = [NSString stringWithFormat:@"%@%@",textField.text,string];
//  
//    //定义谓词
//    //[c]标示不区分大小写
//    NSString *t = [NSString stringWithFormat:@"self like [c]'*%@*'",text];
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:t];
//    
//    //过滤元素
//    self.filterData = [_data filteredArrayUsingPredicate:predicate];
//    
//    //刷新视图
//    [_tableView reloadData];
//    
//    
//    return YES;
//}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    //收起键盘
    [textField resignFirstResponder];
    
    return YES;
    
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值