classview.h
#import <UIKit/UIKit.h>
@interface ClassView : UIView
@property(nonatomic,strong)UITextField *nameTf,*ageTf,*sexTf,*heightTf,*weightTf;
@end
classview.m
#import “ClassView.h”
@implementation ClassView
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self addSubview:self.nameTf];
[self addSubview:self.ageTf];
[self addSubview:self.sexTf];
[self addSubview:self.heightTf];
[self addSubview:self.weightTf];
}
return self;
}
-(UITextField *)nameTf{
if (!_nameTf) {
_nameTf = [[UITextField alloc]initWithFrame:CGRectMake(0, 90, self.frame.size.width, 44)];
_nameTf.placeholder = @"please you name";
_nameTf.textAlignment = NSTextAlignmentCenter;
_nameTf.borderStyle = UITextBorderStyleRoundedRect;
}
return _nameTf;
}
-(UITextField *)ageTf{
if (!_ageTf) {
_ageTf = [[UITextField alloc]initWithFrame:CGRectMake(0, 140, self.frame.size.width, 44)];
_ageTf.placeholder = @"please you age";
_ageTf.textAlignment = NSTextAlignmentCenter;
_ageTf.borderStyle = UITextBorderStyleRoundedRect;
}
return _ageTf;
}
-(UITextField *)sexTf{
if (!_sexTf) {
_sexTf = [[UITextField alloc]initWithFrame:CGRectMake(0, 190, self.frame.size.width, 44)];
_sexTf.placeholder = @"please you sex";
_sexTf.textAlignment = NSTextAlignmentCenter;
_sexTf.borderStyle = UITextBorderStyleRoundedRect;
}
return _sexTf;
}
-(UITextField *)heightTf{
if (!_heightTf) {
_heightTf = [[UITextField alloc]initWithFrame:CGRectMake(0, 240, self.frame.size.width, 44)];
_heightTf.placeholder = @"please you height";
_heightTf.textAlignment = NSTextAlignmentCenter;
_heightTf.borderStyle = UITextBorderStyleRoundedRect;
}
return _heightTf;
}
-(UITextField *)weightTf{
if (!_weightTf) {
_weightTf = [[UITextField alloc]initWithFrame:CGRectMake(0, 300, self.frame.size.width, 44)];
_weightTf.placeholder = @"please you weight";
_weightTf.textAlignment = NSTextAlignmentCenter;
_weightTf.borderStyle = UITextBorderStyleRoundedRect;
}
return _weightTf;
}
@end
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UITableViewController
@end
ViewController.m
#import “ViewController.h”
#import “SecViewController.h”
#import “SqlData.h”
@interface ViewController ()
{
NSMutableArray *array;
}
@end
@implementation ViewController
-
(void)viewDidLoad {
[super viewDidLoad];
//初始化数组
array = [NSMutableArray array];self.title = @“数据库”;
//右侧按钮
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(click)];
//设置行高
self.tableView.rowHeight = 160;
}
//点击按钮方法
-(void)click{
[self.navigationController pushViewController:[SecViewController new] animated:YES];
}
//视图将要展示
-
(void)viewWillAppear:(BOOL)animated{
//调用单利方法创建数据库
[[SqlData initData] initSql];
//调用数据库查询方法
array = [[SqlData initData] array];//关闭数据库
[[SqlData initData] closeSql];//刷新表格
[self.tableView reloadData];
}
#pragma mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return array.count;
}
//设置单元格内容
-
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@“cell”];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@“cell”];
}
//创建ClassName对象
ClassName *classMsg = array[indexPath.row];
//设置内容
cell.textLabel.text = [NSString stringWithFormat:@"%ld\n姓名:%@\n年龄:%@\n性别:%@\n身高:%@\n体重:%@",classMsg.classid,classMsg.name,classMsg.age,classMsg.sex,classMsg.height,classMsg.weitht];
//自动换行
cell.textLabel.numberOfLines = 0;
//返回cell
return cell;
}
//点击单元格方法
-
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
SecViewController *secVc = [[SecViewController alloc] init];
//属性传值
secVc.msg = array[indexPath.row];//跳转到第二个控制器
[self.navigationController pushViewController:secVc animated:YES];
}
//让表格处于编辑状态的方法 删除数据
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//调用数据方法
[[SqlData initData]initSql];
//删除数据是通过主键id
[[SqlData initData] deleteData:[array[indexPath.row]classid]];
//删除数据
[array removeObject:array[indexPath.row]];
//关闭数据库
[[SqlData initData] closeSql];
//刷新表格
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
SecViewController.h
#import <UIKit/UIKit.h>
#import “SqlData.h”
@interface SecViewController : UIViewController
@property(nonatomic,strong)ClassName *msg;
@end
SecViewController.m
#import “SecViewController.h”
#import “ClassView.h”
#import “SqlData.h”
@interface SecViewController ()
{
ClassView *theView;
}
@end
@implementation SecViewController
-
(void)viewDidLoad {
[super viewDidLoad];theView = [[ClassView alloc]initWithFrame:self.view.frame];
theView.backgroundColor = [UIColor lightGrayColor];
self.view = theView;
//修改数据
theView.nameTf.text = self.msg.name;
theView.ageTf.text=self.msg.age;
theView.sexTf.text= self.msg.sex;
theView.heightTf.text= self.msg.height;
theView.weightTf.text=self.msg.weitht;
//如果是添加数据 右侧按钮为save 如果修改数据右侧按钮为Edit
if (!self.msg) {//添加数据 self.title = @"添加数据"; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];
}else{
self.title = @“修改数据”; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(edit)];}
}
//点击save方法 添加数据
-(void)save{
//保存数据
//创建className对象
ClassName *message = [[ClassName alloc] init];
message.name = theView.nameTf.text;
message.age = theView.ageTf.text;
message.sex =theView.sexTf.text;
message.height =theView.heightTf.text;
message.weitht =theView.weightTf.text;
//调用数据方法
[[SqlData initData] initSql];
//调用添加数据方法
[[SqlData initData] addData:message];
//关闭数据库
[[SqlData initData] closeSql];
//返回上一界面
[self.navigationController popViewControllerAnimated:YES];
}
//点击edit方法 修改数据
-(void)edit{
self.msg.name = theView.nameTf.text;
self.msg.age = theView.ageTf.text;
self.msg.sex = theView.sexTf.text;
self.msg.height = theView.heightTf.text;
self.msg.weitht = theView.weightTf.text;
调用数据方法
[[SqlData initData] initSql];
//调用修改数据方法
[[SqlData initData] upData:self.msg];
//关闭数据库
[[SqlData initData] closeSql];
//跳转到上一视图
[self.navigationController popViewControllerAnimated: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