@end
// AppDelegate.m
// Lesson10_tableView编辑
//
// Created by Floating_SH on 15/11/28.
// Copyright © 2015年 SH. All rights reserved.
//
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.window setBackgroundColor:[UIColor whiteColor]];
[self.window makeKeyAndVisible];
[self.window setRootViewController:[[UINavigationController alloc]initWithRootViewController:[RootViewController new]]];
// UINavigationController *RootVC = [[UINavigationController alloc]initWithRootViewController:[RootViewController new]];
// self.window.rootViewController = RootVC;
// [self.window setRootViewController:[RootViewController new]];
return YES;
}
显示的联系人主界面
// RootViewController.h
// Lesson10_tableView编辑
//
// Created by Floating_SH on 15/11/28.
// Copyright © 2015年 SH. All rights reserved.
//
#import
@class Student;
@interface RootViewController : UIViewController
@property (strong,nonatomic)Student *student;
@end
// RootViewController.m
// Lesson10_tableView编辑
//
// Created by Floating_SH on 15/11/28.
// Copyright © 2015年 SH. All rights reserved.
//
#import "RootViewController.h"
#import "Student.h"
#import "DetailViewController.h"
#import "AddDataViewController.h"
@interface RootViewController ()
@property (nonatomic, strong) NSMutableArray *group; //分组名数组
@property (nonatomic, strong) NSMutableDictionary *dataDict; //所有对象的容器
@property(nonatomic,strong) NSIndexPath *indexPath;//这里定义成属性,在下方用来强行调用方法
@property (nonatomic, strong) UITableView *tableView;
@end
//重写cell标识符
static NSString *const systemCellReuseIdentifier = @"systemCellReuseIdentifier";
//设置布尔值 方便下方选择按钮实现不同的方法(删除,添加)
BOOL b = YES;
@implementation RootViewController
#pragma mark ---------------------状态栏按钮--------------------------
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
if(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]){
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(deleteDataAction:)];
UIBarButtonItem *addItem = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(insertDataAction:)];
UIBarButtonItem *moveItem = [[UIBarButtonItem alloc]initWithTitle:@"移动" style:UIBarButtonItemStylePlain target:self action:@selector(moveDataAction:)];
self.navigationItem.rightBarButtonItems = @[addItem,moveItem];
}
return self;
}
#pragma mark------------------tableView编辑实现----------------------
#pragma mark -------------------进入编辑状态--------------------------
//1.让tableView进入编辑状态
- (void)moveDataAction:(UIBarButtonItem *)barButton{
if([barButton.title isEqualToString:@"移动"]){
[self.tableView setEditing:YES animated:YES];
barButton.title = @"完成";
}else{
[self.tableView setEditing:NO animated:YES];
barButton.title = @"移动";
}
}
//2.指定哪些可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//3.完成移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//获取源分区数组
NSMutableArray *array = self.dataDict[self.group[sourceIndexPath.section]];
//找到对应的对象(要移动的元素)
self.student = array[sourceIndexPath.row];
//从原位置移除(同一分区的情况下在同一数组操作)
[array removeObject:_student];//相同的会全部移除
//添加到新位置(同一分区的情况下在同一数组操作)
[array insertObject:_student atIndex:destinationIndexPath.row];
//进行数据交换
}
//补充:限制是否可以跨区移动
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
//如果源分区和目的分区是同一个地方,那么可以返回目的地点作为移动结果
if(sourceIndexPath.section == proposedDestinationIndexPath.section){
return proposedDestinationIndexPath;
}
//否则,滚回老家
return sourceIndexPath;
}
//1.让tableView进入编辑状态
- (void)insertDataAction:(UIBarButtonItem *)barButton{
b = NO;
if([barButton.title isEqualToString:@"添加"]){
[self.tableView setEditing:YES animated:YES];
barButton.title = @"完成";
}else{
[self.tableView setEditing:NO animated:YES];
barButton.title = @"添加";
}
//如果条件成立选第一个,如果不成立选第二个
// barButton.title = self.tableView.editing ? @"完成" : @"添加";
}
//1.让tableView进入编辑状态
- (void)deleteDataAction:(UIBarButtonItem *)barButton{
b = YES;
if ([barButton.title isEqualToString:@"删除"]) {
[self.tableView setEditing:YES animated:YES];
barButton.title = @"完成";
}else{
[self.tableView setEditing:NO animated:YES];
barButton.title = @"删除";
}
}
#pragma mark -------------------设置可编辑区域-------------------------
//2.设置可编辑区域
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
// //前四个分区不可以编辑
// if (indexPath.section <= 3) {
// return NO;
// }
//剩下的可以编辑
return YES;
}
#pragma mark -------------------设定编辑样式--------------------------
//3.设定编辑样式
- (UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
// //前四个分区只能添加
// if (indexPath.section <= 3) {
// return UITableViewCellEditingStyleInsert;
// }
//剩下的分区设为删除样式
if(b){
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleInsert;
//UITableViewCellEditingStyleInsert 是+号
//UITableViewCellEditingStyleDelete 是-号
}
#pragma mark -------------------完成编辑----------------------------
//4.完成编辑(操作UI之前要先操作数据,所有的编辑都是先编辑数据)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
// //获取当前分组名
// NSString *groupName = [self.group objectAtIndex:indexPath.section];
// //根据当前分组名获取当前分区所使用的数组
// NSMutableArray *array = [self.dataDict objectForKey:groupName];
//以上两步书写在一起
NSMutableArray *array = self.dataDict[self.group[indexPath.section]];
if (editingStyle == UITableViewCellEditingStyleDelete) {
//如果当前分区只有一条数据,那么整个分区都删除
if(array.count == 1)
{
//直接将对应的数组从字典中删除(还得将数组中的分组名删除)
[self.dataDict removeObjectForKey:self.group[indexPath.section]];
//※并且删除对应的分组名※
[self.group removeObjectAtIndex:indexPath.section];
//删除UI
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section];
[tableView deleteSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}else
//如果当前分区不只一条数据,那么删除一行就可以
{
//删除要操作的数据
[array removeObjectAtIndex:indexPath.row];
//删除UI(根据当前所操作的位置来删除cell)
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}else if(editingStyle == UITableViewCellEditingStyleInsert){
AddDataViewController *addVC = [AddDataViewController new];
addVC.delegate = self;
self.indexPath = indexPath;
[self.navigationController pushViewController:addVC animated:YES];
//创建新的数据
// NSDictionary *stuDict = @{@"name":@"葫芦娃",@"age":@"666",@"gender":@"未知",@"QQNumber":@"888",@"phoneNumber":@"7777777",@"hobby":@"打妖怪"};
// NSDictionary *stuDict = @{@"name":addVC.nameTextField.text,@"age":addVC.ageTextField.text,@"gender":addVC.genderTextField.text,@"QQNumber":addVC.QQNumberTextField.text,@"phoneNumber":addVC.phoneNumberTextField.text,@"hobby":addVC.hobbyTextView.text};
// Student *student = [[Student alloc]initWithDictionary:stuDict];
//添加数据
// [array addObject:student];
//判断传来的student是否为空,不为空执行下面方法
if(self.student != nil){
[array insertObject:self.student atIndex:indexPath.row + 1];
//创建新的位置
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
//添加UI
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
}
-(void)passValue:(Student *)tempstu{
self.student = tempstu;
//强行调用此方法
[self tableView:self.tableView commitEditingStyle:UITableViewCellEditingStyleInsert forRowAtIndexPath:self.indexPath];
}
- (void)viewDidLoad {
[super viewDidLoad];
//标题
[self.navigationItem setTitle:@"所有联系人"];
#pragma mark -------数据解析------------------
//获取当前应用程序包文件
NSBundle *bundle = [NSBundle mainBundle];
//获取包内属性列表(plist)文件路径
NSString *path = [bundle pathForResource:@"CLASS151042" ofType:@"plist"];
//根据plist文件创建对应的字典
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
//获取所有分组(字典中拿出来的数组是不可变的,然后copy成可变的方便操作)
self.group = [[dict allKeys] mutableCopy];
//排序
[self.group sortUsingSelector:@selector(compare:)];
self.dataDict = [[NSMutableDictionary alloc]initWithCapacity:26];
//遍历字典获取的是key值
for (NSString *key in dict) {
NSMutableArray *tempGroup = [[NSMutableArray alloc]initWithCapacity:30];
// dict[key]-->[dict objectForKey:key];
for (NSDictionary *studentDict in dict[key]) {
Student *stu = [[Student alloc]initWithDictionary:studentDict];
[tempGroup addObject:stu];
}
[self.dataDict setObject:tempGroup forKey:key];
}
// NSLog(@"%@",self.dataDict);
// NSLog(@"%@",[self.dataDict[@"C"]lastObject]);
#pragma mark -------创建表视图------------------
//tableView格式(UITableViewStylePlain是在分组中元素没完之前分组名在左上角)
UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
//通过dataSource设置区和行数
tableView.dataSource = self;
tableView.delegate = self;
//注册cell(注意这边方法的使用)
[tableView registerClass:[UITableViewCell class]forCellReuseIdentifier:systemCellReuseIdentifier];
self.tableView = tableView;
[self.view addSubview:tableView];
}
#pragma mark --------------------tableView代理-----------------------
#pragma mark -------设置分区------------------
//多少分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.group.count;
}
#pragma mark -------设置dataSource必须实现的方法------------------
//每个分区有多少行(设置dataSource必须实现的方法)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//根据当前的分区找到对应的分组名
NSString *key = [self.group objectAtIndex:section];
//根据分组名找到对应的数组
NSArray *array = [self.dataDict objectForKey:key];
//返回数组元素个数作为分区行数
return array.count;
// return [self.dataDict[self.group[section]] count];
}
//设置cell(设置dataSource必须实现的方法)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellIdentifier"];
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:systemCellReuseIdentifier];
//找到对应的分区名
NSString *key = self.group[indexPath.section];
//找到对应的数组
NSArray *group = self.dataDict[key];
//找到数组中的某一个对象
Student *student = group[indexPath.row];
//将数据显示在cell上
cell.textLabel.text = student.name;
cell.detailTextLabel.text = student.phoneNumber;
//在右侧添加call图片
// UIImageView *cellView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"call"]];
// cellView.frame = CGRectMake(320 , 10, 20, 20);
// [cell.contentView addSubview:cellView];
return cell;
}
#pragma mark -------设置头部标题------------------
//设置头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return self.group[section];
}
#pragma mark -------设置右侧索引------------------
//设置右侧索引
- (NSArray
return self.group;
}
#pragma mark -------设置点击cell跳转------------------
//点击cell跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
// NSString *key = self.group[indexPath.section];
//找到对应的数组
// NSArray *group = self.dataDict[key];
//找到数组中的某一个对象
// Student *student = group[indexPath.row];
DetailViewController *detailVC = [[DetailViewController alloc]init];
//将student传到详情页上
detailVC.stu = self.dataDict[self.group[indexPath.section]][indexPath.row];
[self.navigationController pushViewController:detailVC animated:NO];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
联系人详情页
// DetailViewController.h
// Lesson10_tableView编辑
//
// Created by Floating_SH on 15/11/28.
// Copyright © 2015年 SH. All rights reserved.
//
#import
@class Student;
@interface DetailViewController : UIViewController
//将student声明为属性,以便传入student
@property (nonatomic,strong)Student *stu;
@end
// DetailViewController.m
// Lesson10_tableView编辑
//
// Created by Floating_SH on 15/11/28.
// Copyright © 2015年 SH. All rights reserved.
//
#import "DetailViewController.h"
#import "Student.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
#define kWidth self.view.bounds.size.width
#define kHeight self.view.bounds.size.height
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(save)];//save保存
self.navigationItem.rightBarButtonItem = right;
//姓名
UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, kHeight-400, 70, 40)];
NSString *nameStr = [NSString stringWithFormat:@"姓 名 :"];
// nameLabel.borderStyle = UITextBorderStyleRoundedRect;
nameLabel.text = nameStr;
// nameLabel.backgroundColor = [UIColor redColor];
[self.view addSubview:nameLabel];
//姓名编辑框
UITextField *nametextField = [[UITextField alloc]initWithFrame:CGRectMake(110, kHeight-395, kWidth-190, 30)];
NSString *jiastring = [NSString stringWithFormat:@" %@",self.stu.name];
nametextField.borderStyle = UITextBorderStyleRoundedRect;
nametextField.text =jiastring;
[self.view addSubview:nametextField];
//性别
UILabel *genderLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, kHeight-365, 70, 40)];
NSString *genderStr = [NSString stringWithFormat:@"性 别 :"];
genderLabel.text = genderStr;
// genderLabel.backgroundColor = [UIColor blueColor];
[self.view addSubview:genderLabel];
//性别编辑框
UITextField *gendertextField = [[UITextField alloc]initWithFrame:CGRectMake(110, kHeight-360, kWidth-190, 30)];
NSString *genderstring = [NSString stringWithFormat:@" %@",self.stu.gender];
gendertextField.borderStyle = UITextBorderStyleRoundedRect;
gendertextField.text =genderstring;
[self.view addSubview:gendertextField];
//年龄
UILabel *ageLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, kHeight-330, 70, 40)];
NSString *ageStr = [NSString stringWithFormat:@"年 龄 :"];
ageLabel.text = ageStr;
// ageLabel.backgroundColor = [UIColor blueColor];
[self.view addSubview:ageLabel];
//年龄编辑框
UITextField *agetextField = [[UITextField alloc]initWithFrame:CGRectMake(110, kHeight-325, kWidth-190, 30)];
NSString *agestring = [NSString stringWithFormat:@" %@",self.stu.age];
agetextField.borderStyle = UITextBorderStyleRoundedRect;
agetextField.text =agestring;
[self.view addSubview:agetextField];
//手机号
UILabel *phoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, kHeight-295, 70, 40)];
NSString *phoneStr = [NSString stringWithFormat:@"手机号 :"];
phoneLabel.text = phoneStr;
// phoneLabel.backgroundColor = [UIColor blueColor];
[self.view addSubview:phoneLabel];
//手机号编辑框
UITextField *phonetextField = [[UITextField alloc]initWithFrame:CGRectMake(110, kHeight-290, kWidth-190, 30)];
NSString *phonestring = [NSString stringWithFormat:@" %@",self.stu.phoneNumber];
phonetextField.borderStyle = UITextBorderStyleRoundedRect;
phonetextField.text =phonestring;
[self.view addSubview:phonetextField];
//QQ
UILabel *QQNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, kHeight-260, 70, 40)];
NSString *QQNumberStr = [NSString stringWithFormat:@"Q Q :"];
QQNumberLabel.text = QQNumberStr;
// QQNumberLabel.backgroundColor = [UIColor blueColor];
[self.view addSubview:QQNumberLabel];
//QQ编辑框
UITextField *QQtextField = [[UITextField alloc]initWithFrame:CGRectMake(110, kHeight-255, kWidth-190, 30)];
NSString *QQstring = [NSString stringWithFormat:@" %@",self.stu.QQNumber];
QQtextField.borderStyle = UITextBorderStyleRoundedRect;
QQtextField.text = QQstring;
[self.view addSubview:QQtextField];
//爱好
UILabel *hobbyLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, kHeight-220, 70, 40)];
hobbyLabel.text = @"爱 好 :";
[self.view addSubview:hobbyLabel];
UITextView *hobbytextview = [[UITextView alloc]initWithFrame:CGRectMake(110, kHeight-195, kWidth-190, 180)];
NSString *hobbystring = [NSString stringWithFormat:@"%@",self.stu.hobby];
hobbytextview.text = hobbystring;
hobbytextview.layer.borderColor = [[UIColor colorWithRed:215.0 / 255.0 green:215.0 / 255.0 blue:215.0 / 255.0 alpha:1] CGColor];
hobbytextview.layer.borderWidth = 0.7;
hobbytextview.layer.cornerRadius = 6.0;
[self.view addSubview:hobbytextview];
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(40, 70, kWidth-80, kWidth-190)];
NSString *imagestring = [NSString stringWithFormat:@"%@.jpg",self.stu.picture];
imgView.contentMode = UIViewContentModeScaleAspectFit;
UIImage *image = [UIImage imageNamed:imagestring];
imgView.image = image;
[self.view addSubview:imgView];
}
//点击空白处回收键盘(任何UIView子类结束编辑即可)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
-(void)save{
NSLog(@"保存操作");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//添加联系人页面
// AddDataViewController.h
// Lesson10_tableView编辑
//
// Created by Floating_SH on 15/11/30.
// Copyright © 2015年 SH. All rights reserved.
//
#import
@class Student;
@protocol PassValueDelegate
//-(void)passValuename:(NSString *)name age:(NSString *)age gender:(NSString *)gender hobby:(NSString *)hobby phoneNumber:(NSString *)phoneNumber QQNumber:(NSString *)QQNumber;
-(void)passValue:(Student *)tempstu;
@end
@interface AddDataViewController : UIViewController
@property (nonatomic,strong)id
@property (nonatomic,strong)Student *stu;
@property (nonatomic,strong) UITextField *nameTextField;
@property (nonatomic,strong) UITextField *genderTextField;
@property (nonatomic,strong) UITextView *hobbyTextView;
@property (nonatomic,strong) UITextField *phoneNumberTextField;
@property (nonatomic,strong) UITextField *QQNumberTextField;
@property (nonatomic,strong) UITextField *ageTextField;
@end
// AddDataViewController.m
// Lesson10_tableView编辑
//
// Created by Floating_SH on 15/11/30.
// Copyright © 2015年 SH. All rights reserved.
//
#import "AddDataViewController.h"
#import "Student.h"
#import "RootViewController.h"
#define kWidth self.view.bounds.size.width
#define kHeight self.view.bounds.size.height
@interface AddDataViewController ()
@end
@implementation AddDataViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(save)];//save保存
self.navigationItem.rightBarButtonItem = right;
self.stu = [[Student alloc]init];
//姓名
UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, kHeight-400, 70, 40)];
NSString *nameStr = [NSString stringWithFormat:@"姓 名 :"];
// nameLabel.borderStyle = UITextBorderStyleRoundedRect;
nameLabel.text = nameStr;
// nameLabel.backgroundColor = [UIColor redColor];
[self.view addSubview:nameLabel];
//姓名编辑框
self.nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(110, kHeight-395, kWidth-190, 30)];
// NSString *jiastring = [NSString stringWithFormat:@" %@",self.stu.name];
// self.stu.name = nametextField.text;
self.nameTextField.text = @"";
self.nameTextField.borderStyle = UITextBorderStyleRoundedRect;
// nametextField.text =jiastring;
[self.view addSubview:self.nameTextField];
//性别
UILabel *genderLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, kHeight-365, 70, 40)];
NSString *genderStr = [NSString stringWithFormat:@"性 别 :"];
genderLabel.text = genderStr;
// genderLabel.backgroundColor = [UIColor blueColor];
[self.view addSubview:genderLabel];
//性别编辑框
self.genderTextField = [[UITextField alloc]initWithFrame:CGRectMake(110, kHeight-360, kWidth-190, 30)];
// NSString *genderstring = [NSString stringWithFormat:@" %@",self.stu.gender];
// self.stu.gender = gendertextField.text;
self.genderTextField.borderStyle = UITextBorderStyleRoundedRect;
// gendertextField.text =genderstring;
[self.view addSubview:self.genderTextField];
//年龄
UILabel *ageLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, kHeight-330, 70, 40)];
NSString *ageStr = [NSString stringWithFormat:@"年 龄 :"];
ageLabel.text = ageStr;
// ageLabel.backgroundColor = [UIColor blueColor];
[self.view addSubview:ageLabel];
//年龄编辑框
self.ageTextField = [[UITextField alloc]initWithFrame:CGRectMake(110, kHeight-325, kWidth-190, 30)];
// NSString *agestring = [NSString stringWithFormat:@" %@",self.stu.age];
// self.stu.age = agetextField.text;
self.ageTextField.borderStyle = UITextBorderStyleRoundedRect;
// agetextField.text =agestring;
[self.view addSubview:self.ageTextField];
//手机号
UILabel *phoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, kHeight-295, 70, 40)];
NSString *phoneStr = [NSString stringWithFormat:@"手机号 :"];
phoneLabel.text = phoneStr;
// phoneLabel.backgroundColor = [UIColor blueColor];
[self.view addSubview:phoneLabel];
//手机号编辑框
self.phoneNumberTextField = [[UITextField alloc]initWithFrame:CGRectMake(110, kHeight-290, kWidth-190, 30)];
// NSString *phonestring = [NSString stringWithFormat:@" %@",self.stu.phoneNumber];
// self.stu.phoneNumber = phonetextField.text;
self.phoneNumberTextField.borderStyle = UITextBorderStyleRoundedRect;
// phonetextField.text =phonestring;
[self.view addSubview:self.phoneNumberTextField];
//QQ
UILabel *QQNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, kHeight-260, 70, 40)];
NSString *QQNumberStr = [NSString stringWithFormat:@"Q Q :"];
QQNumberLabel.text = QQNumberStr;
// QQNumberLabel.backgroundColor = [UIColor blueColor];
[self.view addSubview:QQNumberLabel];
//QQ编辑框
self.QQNumberTextField = [[UITextField alloc]initWithFrame:CGRectMake(110, kHeight-255, kWidth-190, 30)];
// NSString *QQstring = [NSString stringWithFormat:@" %@",self.stu.QQNumber];
// self.stu.QQNumber = QQNumberLabel.text;
self.QQNumberTextField.borderStyle = UITextBorderStyleRoundedRect;
// QQtextField.text = QQstring;
[self.view addSubview:self.QQNumberTextField];
//爱好
UILabel *hobbyLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, kHeight-220, 70, 40)];
hobbyLabel.text = @"爱 好 :";
[self.view addSubview:hobbyLabel];
self.hobbyTextView = [[UITextView alloc]initWithFrame:CGRectMake(110, kHeight-195, kWidth-190, 180)];
// NSString *hobbystring = [NSString stringWithFormat:@"%@",self.stu.hobby];
// hobbytextview.text = hobbystring;
// self.stu.hobby = hobbytextview.text;
self.hobbyTextView.layer.borderColor = [[UIColor colorWithRed:215.0 / 255.0 green:215.0 / 255.0 blue:215.0 / 255.0 alpha:1] CGColor];
self.hobbyTextView.layer.borderWidth = 0.7;
self.hobbyTextView.layer.cornerRadius = 6.0;
[self.view addSubview:self.hobbyTextView];
// UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(40, 70, kWidth-80, kWidth-190)];
// NSString *imagestring = [NSString stringWithFormat:@"%@.jpg",self.stu.picture];
// imgView.contentMode = UIViewContentModeScaleAspectFit;
// UIImage *image = [UIImage imageNamed:imagestring];
// imgView.image = image;
// [self.view addSubview:imgView];
// [_delegate passValuename:self.nameTextField.text age:self.ageTextField.text gender:self.genderTextField.text hobby:self.hobbyTextView.text phoneNumber:self.phoneNumberTextField.text QQNumber:self.QQNumberTextField.text];
}
//点击空白处回收键盘(任何UIView子类结束编辑即可)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
-(void)save{
self.stu.name = self.nameTextField.text;
self.stu.age = self.ageTextField.text;
self.stu.gender = self.genderTextField.text;
self.stu.hobby = self.hobbyTextView.text;
self.stu.phoneNumber = self.phoneNumberTextField.text;
self.stu.QQNumber = self.QQNumberTextField.text;
[_delegate passValue:self.stu];
NSLog(@"%@保存成功",self.stu.name);
[self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
创建对象
// Student.h
// Lesson10_tableView编辑
//
// Created by Floating_SH on 15/11/28.
// Copyright © 2015年 SH. All rights reserved.
//
#import
@interface Student : NSObject
//写的person和student类都是model (都是在M层)
@property (nonatomic,strong) NSString *name;
@property (nonatomic,strong) NSString *gender;
@property (nonatomic,strong) NSString *age;
@property (nonatomic,strong) NSString *hobby;
@property (nonatomic,strong) NSString *phoneNumber;
@property (nonatomic,strong) NSString *QQNumber;
@property (nonatomic,strong) NSString *picture;
@property (strong,nonatomic) NSString *num_id;
//自定义初始化方法
-(instancetype)initWithDictionary:(NSDictionary *)dict;
@end
// Student.m
// Lesson10_tableView编辑
//
// Created by Floating_SH on 15/11/28.
// Copyright © 2015年 SH. All rights reserved.
//
#import "Student.h"
@implementation Student
//自定义初始化方法
- (instancetype)initWithDictionary:(NSDictionary *)dict{
if(self = [super init]){
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
//打印方法, 辅助校验model对象的属性,查看是否成功
-(NSString *)description{
return [NSString stringWithFormat:@"%@",_name];
}
//重写这个方法 ,将不能正常赋值的key 的value重新赋值给自己定义的(自己)
//重写无法正确赋值的key-Value对,保证KVC对model对象赋值不出错(这里一定要写)
- (void)setValue:(id)value forUndefinedKey:(nonnull NSString *)key{
// if([key isEqualToString:@"id"]){
// _num_id = value;
// }
NSLog(@"=======%@",key);//将不能正常赋值的可以打印出来
}
@end
部分效果图
主界面 主界面