能够实现添加,修改及删除的通讯录
2014-07-19 15:09
190 查看
往通讯录里添加联系人的时候,有一个功能时添加照片,代码中没有写添加链接,只能通过添加已经拖到xcord里照片的名字来把照片显示出来。
#import "MainViewController.h" #import "Student.h" #import "FirstViewController.h" #import "SecondViewController.h" @interface MainViewController ()<PassValueDelegate,InsertViewDelegate> @end @implementation MainViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization self.arr = [NSMutableArray array]; self.title = @"通讯录"; } return self; } - (void)viewDidLoad { [super viewDidLoad]; // 姓名 NSArray *arrName = [NSArray arrayWithObjects:@"小红", @"小华", @"小凯", @"小白", @"小明", @"小博", @"小杨", @"小谢", @"小李", @"小管", @"小美", @"小红", @"小杰",@"小侯", @"小磊", @"莎莎", @"小薛", @"小郑", @"小志", @"小达", @"王贺", @"郭维", @"小童",@"小东", @"小国", nil]; // 电话号码 NSArray *arrNum = [NSArray arrayWithObjects:@"11111",@"22222",@"33333",@"44444",@"55555",@"66666",@"77777",@"88888",@"999999",@"00000", @"12222", @"13333", @"14444", @"15555", @"16666", @"17777", @"18888",@"19999", @"21111",@"123456", @"432143",@"666543", @"987897", @"555667",@"444333", nil]; // 照片 for (int i = 0; i < 25; i++) { Student *stu = [[Student alloc]init]; stu.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", i]]; stu.name = [arrName objectAtIndex:i]; stu.number = [arrNum objectAtIndex:i]; [self.arr addObject:stu]; [stu release]; } self.table = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain]; self.table.delegate = self; self.table.dataSource = self; self.table.backgroundColor = [UIColor magentaColor]; self.table.rowHeight = 40; [self.view addSubview:self.table]; // 是否处在编辑状态 [_table setEditing:NO animated:YES]; // 在bar昨天设置编辑按钮 self.navigationItem.leftBarButtonItem = self.editButtonItem; self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"insert.png"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)]autorelease]; [_table release]; // Do any additional setup after loading the view. } - (void)dealloc { [_arr release]; [_table release]; [super dealloc]; } - (void)buttonAction:(UINavigationBar *)insert { SecondViewController *second = [[SecondViewController alloc]init]; second.delegate = self; [self.navigationController pushViewController:second animated:YES]; } /// 添加方法 - (void)insertStu:(Student *)stu; { [self.arr insertObject:stu atIndex:0]; [[self.view.subviews firstObject]reloadData]; } // viewController的编辑按钮的点击方法 - (void)setEditing:(BOOL)editing animated:(BOOL)animated { [super setEditing:editing animated:animated]; [_table setEditing:editing animated:animated]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.arr.count; } - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { // 获取到原位置的这条数据 NSString *name = [[self.arr objectAtIndex:sourceIndexPath.row] retain]; // 从原来的位置 删除掉这条数据 [self.arr removeObjectAtIndex:sourceIndexPath.row]; // 添加到目的位置 [self.arr insertObject:name atIndex:destinationIndexPath.row]; [name release]; } // 移动 // 设定某一行能否被移动 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *str = @"str"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str]; if (nil == cell) { cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str]autorelease]; } Student *stu = [self.arr objectAtIndex:indexPath.row]; cell.textLabel.text = stu.name; cell.detailTextLabel.text = stu.number; cell.imageView.image = stu.image; return cell; } // tableView的delegate方法 // 点击某一个cell - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { FirstViewController *first = [[FirstViewController alloc]init]; [self.navigationController pushViewController:first animated:YES]; Student *stu = [self.arr objectAtIndex:indexPath.row]; first.str = stu.name; first.str1 = stu.number; first.img1 = stu.image; first.index = indexPath.row; first.delegate = self; [first release]; } // 修改方法 - (void)changeName:(NSString *)name Number:(NSString *)number index:(NSInteger)index { Student *stu = [self.arr objectAtIndex:index]; stu.name = name; stu.number = number; [[self.view.subviews firstObject]reloadData]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
#import <UIKit/UIKit.h> #import "Student.h" @interface MainViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> @property (nonatomic, retain)NSMutableArray *arr; @property (nonatomic, retain)UITableView *table; /// 修改方法 - (void)changeName:(NSString *)name Number:(NSString *)number index:(NSInteger)index; /// 添加方法 - (void)insertStu:(Student *)stu; @end
#import "FirstViewController.h" #import "MainViewController.h" @interface FirstViewController ()<UITextFieldDelegate> @end @implementation FirstViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor magentaColor]; UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(30, 100, 100, 110)]; image.image = self.img1; image.backgroundColor = [UIColor whiteColor]; [self.view addSubview:image]; [image release]; UITextField *textName = [[UITextField alloc]initWithFrame:CGRectMake(200, 100, 100, 30)]; textName.tag = 1000; textName.borderStyle = UITextBorderStyleRoundedRect; textName.text = self.str; textName.backgroundColor = [UIColor whiteColor]; [self.view addSubview:textName]; [textName release]; UILabel *laber = [[UILabel alloc]initWithFrame:CGRectMake(150, 100, 40, 30)]; laber.text = @"姓名"; laber.backgroundColor = [UIColor clearColor]; [self.view addSubview:laber]; UITextField *textNum = [[UITextField alloc]initWithFrame:CGRectMake(200, 180, 100, 30)]; textNum.tag = 2000; textNum.borderStyle = UITextBorderStyleRoundedRect; textNum.delegate = self; textNum.text = self.str1; textNum.backgroundColor = [UIColor whiteColor]; [self.view addSubview:textNum]; [textNum release]; UILabel *laber1 = [[UILabel alloc]initWithFrame:CGRectMake(150, 180, 40, 30)]; laber1.text = @"电话"; laber1.backgroundColor = [UIColor clearColor]; [self.view addSubview:laber1]; [laber release]; UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(130, 300, 60, 30); [button setTitle:@"保存" forState:UIControlStateNormal]; button.backgroundColor = [UIColor whiteColor]; [self.view addSubview:button]; [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; // Do any additional setup after loading the view. } // 键盘收回 - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } // 点击修改后保存实现方法 - (void)buttonClick:(UIButton *)button { UITextField *name = (UITextField *)[self.view viewWithTag:1000]; UITextField *number = (UITextField *)[self.view viewWithTag:2000]; [self.delegate changeName:name.text Number:number.text index:self.index]; }
<pre name="code" class="objc">#import <UIKit/UIKit.h> #import "Student.h" // 指定协议 @protocol PassValueDelegate <NSObject> // 制定方法 - (void)changeName:(NSString *)name Number:(NSString *)number index:(NSInteger)index; @end @interface FirstViewController : UIViewController @property (nonatomic, retain)Student *stu; @property (nonatomic, assign)NSInteger index; @property (nonatomic, copy)NSString *str; @property (nonatomic, copy)NSString *str1; @property (nonatomic, copy)UIImage *img1; @property (nonatomic, assign)id<PassValueDelegate>delegate; @end
#import "SecondViewController.h" #import "Student.h" @interface SecondViewController ()<UITextFieldDelegate> @end @implementation SecondViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization self.stu = [[Student alloc] init]; [_stu release]; } return self; } - (void)dealloc { [_stu release]; [super dealloc]; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor magentaColor]; UITextField *textImg = [[UITextField alloc]initWithFrame:CGRectMake(30, 100, 100, 110)]; textImg.tag = 3000; textImg.delegate = self; textImg.borderStyle = UITextBorderStyleRoundedRect; textImg.backgroundColor = [UIColor whiteColor]; [self.view addSubview:textImg]; [textImg release]; UITextField *textName = [[UITextField alloc]initWithFrame:CGRectMake(200, 100, 100, 30)]; textName.tag = 1000; textName.delegate = self; textName.borderStyle = UITextBorderStyleRoundedRect; textName.backgroundColor = [UIColor whiteColor]; [self.view addSubview:textName]; [textName release]; UILabel *laber = [[UILabel alloc]initWithFrame:CGRectMake(150, 100, 40, 30)]; laber.text = @"姓名"; laber.backgroundColor = [UIColor clearColor]; [self.view addSubview:laber]; UITextField *textNum = [[UITextField alloc]initWithFrame:CGRectMake(200, 180, 100, 30)]; textNum.tag = 2000; textNum.delegate = self; textNum.borderStyle = UITextBorderStyleRoundedRect; textNum.backgroundColor = [UIColor whiteColor]; [self.view addSubview:textNum]; [textNum release]; UILabel *laber1 = [[UILabel alloc]initWithFrame:CGRectMake(150, 180, 40, 30)]; laber1.text = @"电话"; laber1.backgroundColor = [UIColor clearColor]; [self.view addSubview:laber1]; [laber release]; UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(130, 300, 60, 30); [button setTitle:@"保存" forState:UIControlStateNormal]; button.backgroundColor = [UIColor whiteColor]; [self.view addSubview:button]; [button addTarget:self action:@selector(insertStu:) forControlEvents:UIControlEventTouchUpInside]; // Do any additional setup after loading the view. } - (void)insertStu:(Student *)stu; { UITextField *name = (UITextField *)[self.view viewWithTag:1000]; UITextField *number = (UITextField *)[self.view viewWithTag:2000]; UITextField *img = (UITextField *)[self.view viewWithTag:3000]; self.stu.name = name.text; self.stu.number = number.text; self.stu.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",img.text]]; [self.delegate insertStu:self.stu]; [self.navigationController popToRootViewControllerAnimated:YES]; } // 键盘收回 - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; }
#import <UIKit/UIKit.h> #import "Student.h" @protocol InsertViewDelegate <NSObject> - (void)insertStu:(Student *)stu; @end @interface SecondViewController : UIViewController @property (nonatomic, retain)Student *stu; @property (nonatomic, assign)id<InsertViewDelegate>delegate; @end
#import "Student.h" @implementation Student - (id)initwithName:(NSString *)name Number:(NSString *)number Image:(UIImage *)image { self = [super init]; if ( self) { _name = name; _number = number; _image = image; } return self; } - (void)dealloc { [_name release]; [_number release]; [_image release]; [super dealloc]; } @end
#import <Foundation/Foundation.h> @interface Student : NSObject @property (nonatomic, retain)NSString *name; @property (nonatomic, retain)NSString *number; @property (nonatomic, retain)UIImage *image; - (id)initwithName:(UIImage *)name Number:(NSString *)number Image:(NSString *)image; @end
相关文章推荐
- 【通讯录系统】:实现一个通讯录(可添加、删除、修改、查找、显示、清空、排序)
- 用线性表编写通讯录管理系统 实现添加,查询,修改,删除功能
- c语言版通讯录 实现联系人信息的添加,删除,修改,排序,保存,查询
- 用C语言模拟实现一个通讯录,要求实现其添加、删除、修改、查找、显示和排序联系人信息的功能
- 【通讯录系统】:实现一个通讯录(可添加、删除、修改、查找、显示、清空、排序)
- 用c语言实现一个通讯录; 通讯录可以存储1000个人的信息,信息包括姓名、性别、年龄、电话、住址 提供方法: 1.添加联系人信息 2.删除指定联系人信息 3.查找指定联系人信息 4.修改指定联系&
- 稳扎稳打Silverlight(58) - 4.0通信之WCF RIA Services: 通过 Domain Service, 以 MVVM 模式实现数据的添加、删除、修改和查询
- 稳扎稳打Silverlight(58) - 4.0通信之WCF RIA Services: 通过 Domain Service, 以 MVVM 模式实现数据的添加、删除、修改和查询
- 利用ErlyDB、Mysql来实现表的添加、删除、修改、查询操作
- 用Nhibernate怎么实现数据的添加、删除、修改简单程序
- 稳扎稳打Silverlight(58) - 4.0通信之WCF RIA Services: 通过 Domain Service, 以 MVVM 模式实现数据的添加、删除、修改和查询
- 一个实现添加、修改、删除记录的类
- 稳扎稳打Silverlight(58) - 4.0通信之WCF RIA Services: 通过 Domain Service, 以 MVVM 模式实现数据的添加、删除、修改和查询
- 用Nhibernate怎么实现数据的添加、删除、修改简单程序
- 一个实现添加、修改、删除记录的类
- 稳扎稳打Silverlight(58) - 4.0通信之WCF RIA Services: 通过 Domain Service, 以 MVVM 模式实现数据的添加、删除、修改和查询
- C#写的对系统用户和组操作的类(可以实现用户和组的添加、删除、修改)
- 用Nhibernate怎么实现数据的添加、删除、修改简单程序
- 稳扎稳打Silverlight(58) - 4.0通信之WCF RIA Services: 通过 Domain Service, 以 MVVM 模式实现数据的添加、删除、修改和查询
- VB数据库编程实现添加、查询、修改、删除