电话本的检索功能

Posted pengyuan_D

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了电话本的检索功能相关的知识,希望对你有一定的参考价值。

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

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;
    




以上是关于电话本的检索功能的主要内容,如果未能解决你的问题,请参考以下文章

在 Android 中使用 ContactsContract 检索电话号码 - 功能不起作用

Facebook iOS SDK:检索用户的电话号码和国家

检索电话号码

如何通过 LookUpKey 获取联系电话号码?

android 车机电话的通讯录联系人搜索实现解析 ------- 填坑日记

android 车机电话的通讯录联系人搜索实现解析 ------- 填坑日记