
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];
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) {
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
{
_searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
[_searchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
_tableView.tableHeaderView = _searchBar;
_searchDisplayCtal = [[UISearchDisplayController alloc]initWithSearchBar:_searchBar contentsController:self];
[_searchDisplayCtal setSearchResultsDelegate:self];
[_searchDisplayCtal setSearchResultsDataSource:self];
}
-(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];
}
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];
}
@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];
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];
_textTouch.borderStyle=UITextBorderStyleRoundedRect;
_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];
_textNumber.borderStyle=UITextBorderStyleRoundedRect;
_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 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];
}
@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];
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];
_textTouch.borderStyle=UITextBorderStyleRoundedRect;
_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];
_textNumber.borderStyle=UITextBorderStyleRoundedRect;
_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 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];
}
@end