能够实现添加,修改及删除的通讯录
来源:互联网 发布:淘宝大c香港站是正品吗 编辑:程序博客网 时间:2024/05/01 17:06
往通讯录里添加联系人的时候,有一个功能时添加照片,代码中没有写添加链接,只能通过添加已经拖到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
0 0
- 能够实现添加,修改及删除的通讯录
- 【通讯录系统】:实现一个通讯录(可添加、删除、修改、查找、显示、清空、排序)
- Swift - 添加、修改、删除通讯录联系人
- 用C语言模拟实现一个通讯录,要求实现其添加、删除、修改、查找、显示和排序联系人信息的功能
- 用c语言实现一个通讯录; 通讯录可以存储1000个人的信息,信息包括姓名、性别、年龄、电话、住址 提供方法: 1.添加联系人信息 2.删除指定联系人信息 3.查找指定联系人信息 4.修改指定联系&
- 实现添加删除排序修改
- 汇编实现的简单的通讯录系统,有插入,修改,删除,打印,查找功能
- 打造一个在线通信录,实现在线通讯录的显示、添加、删除
- UITableView 编辑(一个实现了添加 删除功能的iOS通讯录)
- UIday1002:UITableView 2 用UITableView实现通讯录cell简单的数据移动、删除、添加功能
- js实现class的样式的修改、添加、删除
- ios实现通讯录的查询与删除
- ios实现通讯录的查询与删除
- ios实现通讯录的查询与删除
- ios实现通讯录的查询与删除
- lucene功能四;索引库的维护;索引库的添加;修改;删除;及代码实现;代码示例
- 一个实现添加、修改、删除记录的类
- 用datagrid 的 Footer实现 添加修改和删除功能
- 黑马程序员Java学习日记(2)面向对象
- 北碚新城驾校
- Hadoop安装(含client安装)
- 17级强台风“威马逊”袭击华南
- 七月份 CUGBACM_Summer_Tranning 题解
- 能够实现添加,修改及删除的通讯录
- poj3259 Wormholes 图的负权回路判定,Bellman_Ford
- wikioi 2147 bitset+map解决
- 世界杯Again!——并查集 and STL(其实完全可以不用并查集)
- 《TCP/IP详解卷2:实现》笔记--IP选项处理
- POJ 3614 Sunscreen 贪心 优先队列
- 图的概念和关于图的几个算法
- Android内核sysfs中switch类使用实例
- Hopscotch