代码演示:
AddressBookModel.m 用于创建第一个页面 包括cell的自定义高度问题
1 #import "ViewController.h" 2 #import "TableViewCell.h" 3 #import "DetailViewController.h" 4 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate,DetailVCDelegate> 5 6 7 @property (weak, nonatomic) IBOutlet UITableView *tableView; 8 @property(nonatomic,strong)NSMutableArray *dataArray; 9 10 11 @end 12 13 @implementation ViewController 14 15 - (void)viewDidLoad { 16 [super viewDidLoad]; 17 self.dataArray = [NSMutableArray array]; 18 19 //预估tableViewCell的高度 20 self.tableView.estimatedRowHeight = 120; 21 //自适应高度(以后用这个) 22 self.tableView.rowHeight = UITableViewAutomaticDimension; 23 24 // self.navigationController.navigationBar.translucent = NO; 25 26 } 27 28 - (IBAction)insertbutton:(UIBarButtonItem *)sender { 29 DetailViewController *detailVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ABC"]; 30 31 //数据传入第一个界面的代理 32 detailVC.delegate = self; 33 34 [self.navigationController pushViewController:detailVC animated:YES]; 35 36 } 37 38 - (IBAction)deleteButton:(UIBarButtonItem *)sender { 39 40 [self.tableView setEditing:!self.tableView.isEditing animated:YES]; 41 42 43 } 44 45 46 //代理方法的实现 47 -(void)passValueDelegate:(AddressBookModel *)passModel 48 { 49 //代理方法传过来的值添加到数组中 50 [self.dataArray addObject:passModel]; 51 //刷新tableView 52 [self.tableView reloadData]; 53 54 } 55 56 57 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 58 { 59 return self.dataArray.count; 60 61 } 62 63 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 64 { 65 //注意 这里是TableViewCell记清楚 66 TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath]; 67 68 cell.model = self.dataArray[indexPath.row]; 69 // [self.tableView reloadData]; 70 71 return cell; 72 } 73 74 75 //点击cell,返回第二个界面进行信息的修改 76 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 77 { 78 DetailViewController *detailVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ABC"]; 79 //属性赋值第二步 把dataArray赋值给属性传值的对象 80 detailVC.model = self.dataArray[indexPath.row]; 81 82 //修改值的代理 83 detailVC.delegate = self; 84 //传递indexPath.row的作用是再传回来,作为代理方法中的参数使用 85 detailVC.index = indexPath.row; 86 87 [self.navigationController pushViewController:detailVC animated:YES]; 88 89 } 90 //修改值的代理方法 91 -(void)changeValueToListVC:(AddressBookModel *)passModel index:(NSInteger)index 92 { 93 //替换数组 index位置的元素 为新的model 94 [self.dataArray replaceObjectAtIndex:index withObject:passModel]; 95 // 刷新tableView 96 [self.tableView reloadData]; 97 98 } 99 100 101 102 103 104 105 // 设置哪个cell可以编辑 106 -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 107 { 108 return self; 109 } 110 //真正的执行编辑的事情 111 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 112 { 113 //在编辑时,以一定要先处理好数据源,然后再处理view 114 [self.dataArray removeObjectAtIndex:indexPath.row]; 115 //tableView 删除一个cell的方法,第一个参数代表 删除哪个分区的cell 第二个参数代表 删除时的动画 116 [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 117 118 119 }
TableViewCell.h 和 TableViewCell.m 文件 用于第一个界面的Model类的定义
1 #import <UIKit/UIKit.h> 2 #import "AddressBookModel.h" 3 @interface TableViewCell : UITableViewCell 4 5 @property (weak, nonatomic) IBOutlet UIImageView *image; 6 7 @property (weak, nonatomic) IBOutlet UILabel *lable1; 8 9 10 @property (weak, nonatomic) IBOutlet UILabel *lable2; 11 12 @property (weak, nonatomic) IBOutlet UILabel *lable3; 13 14 @property (weak, nonatomic) IBOutlet UILabel *lable4; 15 16 @property (weak, nonatomic) IBOutlet UILabel *content; 17 18 19 @property(nonatomic,strong)AddressBookModel *model; 20 21 @end
1 #import "TableViewCell.h" 2 3 4 5 6 @implementation TableViewCell 7 8 //重写model的setter方法 将model接收到的值,赋给本身的lable********************* 9 -(void)setModel:(AddressBookModel *)model 10 { 11 self.lable1.text = model.name; 12 self.lable2.text = model.age; 13 self.lable3.text = model.sex; 14 self.lable4.text = model.phone; 15 self.content.text = model.content; 16 // 把NSData转换成image类型 **** 17 self.image.image = [UIImage imageWithData:model.image]; 18 19 20 } 21 22 @end
DetailViewController.h文件 用于第二个详情界面的属性定义
1 #import <UIKit/UIKit.h> 2 #import "AddressBookModel.h" 3 4 @protocol DetailVCDelegate <NSObject> 5 6 //传值的代理方法 7 -(void)passValueDelegate:(AddressBookModel *)passModel; 8 //修改的代理方法 9 -(void)changeValueToListVC:(AddressBookModel *)passModel index:(NSInteger)index; 10 11 @end 12 13 14 15 @interface DetailViewController : UIViewController 16 17 @property (weak, nonatomic) IBOutlet UIButton *image; 18 @property (weak, nonatomic) IBOutlet UITextField *lable1; 19 @property (weak, nonatomic) IBOutlet UITextField *lable2; 20 21 @property (weak, nonatomic) IBOutlet UITextField *lable3; 22 23 @property (weak, nonatomic) IBOutlet UITextField *lable4; 24 25 @property (weak, nonatomic) IBOutlet UITextView *content; 26 27 28 //代理传值的属性1 29 @property(nonatomic,assign)id<DetailVCDelegate>delegate; 30 31 //属性传值 32 @property(nonatomic,strong)AddressBookModel *model; 33 34 //代理传值的属性2 35 @property(nonatomic,assign)NSInteger index; 36 37 38 @end
DetailViewController.m文件 第二个界面设置,一二界面的传值,以及图片的设置显示
1 #import "DetailViewController.h" 2 #import "AddressBookModel.h" 3 @interface DetailViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate> 4 //设置照片属性 5 @property(nonatomic,strong)NSData *imageDate; 6 7 @end 8 9 @implementation DetailViewController 10 11 - (void)viewDidLoad { 12 [super viewDidLoad]; 13 // Do any additional setup after loading the view. 14 15 self.navigationController.navigationBar.translucent = NO; 16 [self loadLTview]; 17 } 18 19 20 -(void)loadLTview 21 { 22 self.lable1.text = self.model.name; 23 24 self.lable2.text = self.model.age; 25 self.lable3.text = self.model.sex; 26 self.lable4.text = self.model.phone; 27 self.content.text = self.model.content; 28 29 // 把前一页面传入的NSData类型的数据转换成image ***************** 30 [self.image setImage:[UIImage imageWithData:self.model.image] forState:UIControlStateNormal]; 31 32 } 33 34 35 36 - (IBAction)saveButtonClick:(UIButton *)sender { 37 //获取到所有可输入控件上的输入的数据 38 NSString *nameStr = self.lable1.text; 39 NSString *ageStr = self.lable2.text; 40 NSString *sexStr = self.lable3.text; 41 NSString *phoneStr = self.lable4.text; 42 NSString *contentStr = self.content.text; 43 44 45 //初始化一个model 传输照片时,要注意这里的格式*********** 46 AddressBookModel *model = [[AddressBookModel alloc] initWithName:nameStr age:ageStr sex:sexStr pnone:phoneStr content:contentStr image:self.imageDate]; 47 48 // 判断代理人是否存在,代理人是否注册了passValueDelegate 和 49 if (self.delegate && [self.delegate respondsToSelector:@selector(passValueDelegate:)] && [self.delegate respondsToSelector:@selector(changeValueToListVC:index:)]) { 50 // 如果self.model是否存在,如果存在 则判定为是修改联系人,所以 触发changeValueToListVC:index:方法 51 if (self.model != nil) { 52 // 填入的两个参数,第一个是model 注意 这个model是自己创建的 不是属性的model 第二个参数 index。告诉上一个页面需要替换数组中哪个位置的元素 53 [self.delegate changeValueToListVC:model index:self.index]; 54 } else{ 55 // 直接把自己创建的model 返回去 56 [self.delegate passValueDelegate:model]; 57 } 58 } 59 60 [self.navigationController popToRootViewControllerAnimated:YES]; 61 } 62 63 64 65 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 66 { 67 [self.view endEditing:YES]; 68 69 } 70 71 72 //照片的设置------------------------------------------------------------ 73 - (IBAction)imagePickerButtonVlicked:(UIButton *)sender { 74 75 UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"选取照片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相机",@"相册", nil]; 76 77 [action showFromRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) inView:self.view animated:YES]; 78 } 79 80 81 -(void)setImagePickerController:(UIImagePickerControllerSourceType)source 82 { 83 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; 84 85 imagePicker.sourceType = source; 86 87 88 imagePicker.delegate = self; 89 90 91 92 [imagePicker setEditing:YES]; 93 imagePicker.allowsEditing = YES; 94 95 //模态推出视图 96 [self presentViewController:imagePicker animated:YES completion:nil]; 97 98 } 99 100 -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 101 { 102 switch (buttonIndex) { 103 case 0: 104 [self setImagePickerController:UIImagePickerControllerSourceTypeCamera]; 105 break; 106 case 1: 107 [self setImagePickerController:UIImagePickerControllerSourceTypePhotoLibrary]; 108 break; 109 110 default: 111 break; 112 } 113 114 } 115 116 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 117 { 118 119 //如果图片编辑之后 需要使用 UIImagePickerControllerEditedImage 120 //如果图片没有编辑 只需获取 UIImagePickerControllerOriginalImage 121 // UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 122 UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"]; 123 124 //UIImagePNGRepresentation 把图片转化成PNG格式 不压缩比较大 125 // UIImageJPEGRepresentation 把图片转化成JPEG的格式,第二个参数填的是一个压缩的系数,一般是 0.5 126 // NSDate *data = UIImagePNGRepresentation(<#UIImage *image#>); 127 128 //照片传递到上一界面时,这里要进行数据的转化*** 129 self.imageDate = UIImageJPEGRepresentation(image, 0.5); 130 131 [self.image setImage:image forState:UIControlStateNormal]; 132 133 [picker dismissViewControllerAnimated:YES completion:nil]; 134 135 }
AddressBookModel.h 和 AddressBookModel.m主要作用创建公共属性以供调用或使用
1 #import <Foundation/Foundation.h> 2 3 @interface AddressBookModel : NSObject 4 5 @property(nonatomic,copy)NSString *name; 6 @property(nonatomic,copy)NSString *age; 7 @property(nonatomic,copy)NSString *sex; 8 @property(nonatomic,copy)NSString *phone; 9 @property(nonatomic,copy)NSString *content; 10 @property(nonatomic,copy)NSData *image; 11 12 13 -(id)initWithName:(NSString *)nameStr 14 age:(NSString *)age 15 sex:(NSString *)sex 16 pnone:(NSString *)phone 17 content:(NSString *)content 18 image:(NSData *)image; 19 20 21 22 23 @end
1 #import "AddressBookModel.h" 2 3 @implementation AddressBookModel 4 5 -(id)initWithName:(NSString *)nameStr age:(NSString *)age sex:(NSString *)sex pnone:(NSString *)phone content:(NSString *)content image:(NSData *)image 6 { 7 8 self = [super init]; 9 if (self) { 10 _name = nameStr; 11 _age = age; 12 _sex = sex; 13 _phone = phone; 14 _content = content; 15 _image = image; 16 17 } 18 19 return self; 20 21 } 22 23 24 25 26 @end
界面搭建图:
效果图:
时间: 2024-09-29 21:54:24