往通讯录里添加联系人的时候,有一个功能时添加照片,代码中没有写添加链接,只能通过添加已经拖到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