联系人添加查找修改(有搜索功能)(OC)

这篇博客详细介绍了如何在iOS应用中实现联系人的搜索功能,包括创建联系人主界面、添加新联系人以及修改已存在联系人的流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述

1.具有搜索功能

这里写图片描述

2.联系人主界面

#import "ViewController.h"
#import "First_ViewController.h"
#import "Add_ViewController.h"
#import "AppDelegate.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
//展示所有数据的表格视图
@property (retain) UITableView* tableView;
//表格的数据源,存主标题
@property (retain) NSMutableArray* SourceArr;
//存副标题
@property (retain) NSMutableArray* SourceArr1;

//存放搜索结果的数组
@property (retain) NSMutableArray* resultArray;
//类似于文本框的控件,专门用于搜索功能
@property (retain) UISearchBar* searchBar;
//搜索控制器
@property (retain) UISearchDisplayController* searchDisplayCtal;
@end

@implementation ViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    //本地永久性保存
    NSUserDefaults* ud = [NSUserDefaults standardUserDefaults];

    UIApplication* app = [UIApplication sharedApplication];
    AppDelegate* delegate = (AppDelegate*)app.delegate;
    if (delegate.Linkman2.length > 0 && delegate.PhoneCode2.length > 0) {
        _SourceArr[delegate.index] = delegate.Linkman2;
        _SourceArr1[delegate.index] = delegate.PhoneCode2;
        NSLog(@">>%@<<",_SourceArr[delegate.index]);
        NSLog(@">>%ld<<",(long)delegate.index);
        [ud setObject:_SourceArr forKey:@"relationUD"];
        [ud setObject:_SourceArr1 forKey:@"numberUD"];
        //更新
        [_tableView reloadData];
    }
    if (delegate.Relation.length > 0 && delegate.Number.length > 0) {
        [_SourceArr addObject:delegate.Relation];
        [_SourceArr1 addObject:delegate.Number];
        [ud setObject:_SourceArr forKey:@"relationUD"];
        [ud setObject:_SourceArr1 forKey:@"numberUD"];
        NSLog(@"qq%@qq",ud);
        //更新
        [_tableView reloadData];
    }
    delegate.Linkman2 = @"";
    delegate.PhoneCode2 = @"";
    delegate.Relation = @"";
    delegate.Number = @"";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    //防止下滑
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = @"联系人";


    //给可变数组开辟空间
    _SourceArr = [NSMutableArray new];
    _SourceArr1 = [NSMutableArray new];
    _resultArray = [NSMutableArray new];

    //构建表格视图
    [self Tabulation];
    //创建搜索框
    [self SearchFor];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStyleDone target:self action:@selector(rightAction:)];
}

-(void)Tabulation
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];

    //保存======================
    //本地永久性保存
    NSUserDefaults* ud = [NSUserDefaults standardUserDefaults];
    NSArray* relatuD = [ud objectForKey:@"relationUD"];
    [_SourceArr addObjectsFromArray:relatuD];
    NSArray* numuD = [ud objectForKey:@"numberUD"];
    [_SourceArr1 addObjectsFromArray:numuD];
    //==========================

    if (relatuD.count <= 0 || relatuD == nil) {
        //NSArray* arr = @[@"赵",@"钱",@"孙",@"李",@"周",@"吴",@"胡",@"刘",@"陈",@"易"];
        for (NSInteger i = 0; i < 10; i++) {
            NSString* str = [NSString stringWithFormat:@"瓦力%ld号",(long)i+1];
            [_SourceArr addObject:str];

            NSString* str1 = [NSString stringWithFormat:@"1577967982%ld",(long)i];
            [_SourceArr1 addObject:str1];
        }
    }

    //设置代理
    _tableView.delegate = self;
    _tableView.dataSource = self;
}

-(void)SearchFor
{
    // 实例化搜索框控件,默认高度是44
    _searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
    [_searchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    //把搜索框设置到头部视图
    _tableView.tableHeaderView = _searchBar;
    // 如果想使用searchBar的回调方法,也要遵守协议并设置委托对象
    //    _searchBar.delegate = self;

    /*
     第一个参数,是一个UISearchBar对象,用于输入搜索信息
     第二个参数,是一个视图控制器对象,这个控制器对象必须遵守表格视图的两个协议(delegate,dataSource)
     UISearchDisplayController对象实际上自带一个表格视图searTableView,这个表格视图负责加载当前搜索的信息,这些信息由第二个参数负责帮忙展示出来,所以 第二个参数一定是遵守表格视图的两个协议的
     【注】当前有两个表格视图,一个是我们自己构建的那个_tableView 还有一个是搜索器自带的searchTableView,这两个表格视图共用一个视图控制器(self)
     */
    _searchDisplayCtal = [[UISearchDisplayController alloc]initWithSearchBar:_searchBar contentsController:self];
    // 设置搜索控制器的委托(实际上就是设置搜索控制器自带的表格视图的委托对象)两个视图都要跟self构建委托关系,self才能帮两个表格视图加载数据
    [_searchDisplayCtal setSearchResultsDelegate:self];
    [_searchDisplayCtal setSearchResultsDataSource:self];

}

//设置标题,因为标题在数据源的小数组里面,所有必须先取到小数组,才能取到真正的标题内容,小数组的获取可以通过区的索引值来获取,标题可以通过行的索引值来获取,也就是indexPath, indexPath里面就包含了当前单元格的区索引值和行的索引值,indexPath.section是曲的索引值,indexPath.row是行的索引值
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* cellID = @"cell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    // 设置cell内容
    if (tableView != _tableView) {
        // 如果是展示搜索结果的表视图,则加载搜索结果的内容
        cell.textLabel.text = _resultArray[indexPath.row];
        cell.detailTextLabel.text = _SourceArr1[indexPath.row];
    }else{
        //显示主标题
        cell.textLabel.text = _SourceArr[indexPath.row];
        cell.detailTextLabel.text = _SourceArr1[indexPath.row];
    }
    return cell;
}

//可以监听到当前用户点击的是哪一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取消选中行
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    First_ViewController* firstVC = [[First_ViewController alloc]init];
    UIApplication* app = [UIApplication sharedApplication];
    AppDelegate* delegate = (AppDelegate*)app.delegate;
    delegate.Linkman1 = [NSString stringWithFormat:@"%@", _SourceArr[indexPath.row]];
    delegate.PhoneCode1 = [NSString stringWithFormat:@"%@",_SourceArr1[indexPath.row]];
    delegate.index = indexPath.row;
    NSLog(@"==%@==", delegate.Linkman1);
    NSLog(@"--%@--", delegate.PhoneCode1);
    [self.navigationController pushViewController:firstVC animated:YES];
}


-(void)rightAction:(UIBarButtonItem*)sender
{
    Add_ViewController* addVC = [[Add_ViewController alloc]init];
    [self.navigationController pushViewController:addVC animated:YES];
}

#pragma mark- UITableViewDataSource
//返回行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // 判断当前展示数据的表视图是不是要展示数据源的那个表视图
    if (tableView != _tableView) {
        // 先把上次搜索的临时数据清空
        [_resultArray removeAllObjects];
        // 获取当前搜索框内的信息
        NSString* searchMessage = _searchBar.text;

        for (NSString* data in _SourceArr) {
            // 查看每一个数据项内是否包含当前搜索框内的字符串
            NSRange range = [data rangeOfString:searchMessage];
            if (range.location != NSNotFound) {
                [_resultArray addObject:data];
            }
        }
        // 如果不是,则要展示的是当前的搜索结果
        return _resultArray.count;
    }else
    return _SourceArr.count;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

这里写图片描述

3.添加联系人

#import "Add_ViewController.h"
#import "AppDelegate.h"

@interface Add_ViewController ()
@property (retain) UITextField* textTouch;
@property (retain) UITextField* textNumber;

@end

@implementation Add_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //搭建界面
    [self Interface];
}

-(void)Interface
{
    _textTouch = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, [UIScreen mainScreen].bounds.size.width-100, 45)];
    //设置提示信息
    _textTouch.placeholder=@"请输入联系人姓名";
    _textTouch.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_textTouch];
    //设置textFiel的类型,UITextBorderStyleRoundedRect 圆角类型
    _textTouch.borderStyle=UITextBorderStyleRoundedRect;
    //设置清除按钮的显示模式
    //UITextFieldViewModeAlways 总是出现清除按钮
    _textTouch.clearButtonMode=UITextFieldViewModeAlways;

    _textNumber = [[UITextField alloc]initWithFrame:CGRectMake(50, 160, [UIScreen mainScreen].bounds.size.width-100, 45)];
    _textNumber.placeholder = @"请输入电话号码";
    _textNumber.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_textNumber];
    //设置textFiel的类型,UITextBorderStyleRoundedRect 圆角类型
    _textNumber.borderStyle=UITextBorderStyleRoundedRect;
    //设置清除按钮的显示模式
    //UITextFieldViewModeAlways 总是出现清除按钮
    _textNumber.clearButtonMode=UITextFieldViewModeAlways;

    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(80, 220, [UIScreen mainScreen].bounds.size.width-160, 45);
    btn.backgroundColor = [UIColor grayColor];
    //设置圆角显现
    btn.layer.masksToBounds = YES;
    //设置圆角的角度大小
    btn.layer.cornerRadius = 10;
    [btn setTitle:@"保存" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

    //创建点击手势
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    //给self.view 加手势
    [self.view addGestureRecognizer:tap];
}

//点击手势
-(void)tapAction:(UITapGestureRecognizer*)sender
{
    [_textTouch resignFirstResponder];
    [_textNumber resignFirstResponder];
}

-(void)btnAction:(UIButton*)sender
{
    UIApplication* app = [UIApplication sharedApplication];
    AppDelegate* delegare = (AppDelegate*)app.delegate;
    if (_textTouch.text.length > 0 && _textNumber.text.length > 0) {
        delegare.Relation = _textTouch.text;
        delegare.Number = _textNumber.text;
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

这里写图片描述

4.修改联系人

#import "First_ViewController.h"
#import "AppDelegate.h"

@interface First_ViewController ()<UITextFieldDelegate>
@property (retain) UITextField* textTouch;
@property (retain) UITextField* textNumber;
@end

@implementation First_ViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    UIApplication* app = [UIApplication sharedApplication];
    AppDelegate* delegate = (AppDelegate*)app.delegate;
    _textTouch.text = delegate.Linkman1;
    _textNumber.text = delegate.PhoneCode1;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //搭建界面
    [self Interface];
}

-(void)Interface
{
    _textTouch = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, [UIScreen mainScreen].bounds.size.width-100, 45)];
    _textTouch.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_textTouch];
    //设置textFiel的类型,UITextBorderStyleRoundedRect 圆角类型
    _textTouch.borderStyle=UITextBorderStyleRoundedRect;
    //设置清除按钮的显示模式
    //UITextFieldViewModeAlways 总是出现清除按钮
    _textTouch.clearButtonMode=UITextFieldViewModeAlways;

    _textNumber = [[UITextField alloc]initWithFrame:CGRectMake(50, 160, [UIScreen mainScreen].bounds.size.width-100, 45)];
    _textNumber.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_textNumber];
    //设置textFiel的类型,UITextBorderStyleRoundedRect 圆角类型
    _textNumber.borderStyle=UITextBorderStyleRoundedRect;
    //设置清除按钮的显示模式
    //UITextFieldViewModeAlways 总是出现清除按钮
    _textNumber.clearButtonMode=UITextFieldViewModeAlways;

    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(80, 220, [UIScreen mainScreen].bounds.size.width-160, 45);
    btn.backgroundColor = [UIColor grayColor];
    //设置圆角显现
    btn.layer.masksToBounds = YES;
    //设置圆角的角度大小
    btn.layer.cornerRadius = 10;
    [btn setTitle:@"保存" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

    //创建点击手势
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    //给self.view 加手势
    [self.view addGestureRecognizer:tap];
}

//点击手势
-(void)tapAction:(UITapGestureRecognizer*)sender
{
    [_textTouch resignFirstResponder];
    [_textNumber resignFirstResponder];
}

-(void)btnAction:(UIButton*)sender
{
    UIApplication* app = [UIApplication sharedApplication];
    AppDelegate* delegate = (AppDelegate*)app.delegate;
    if (_textTouch.text.length > 0 && _textNumber.text.length > 0) {
        delegate.Linkman2 = _textTouch.text;
        delegate.PhoneCode2 = _textNumber.text;
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值