sql

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值