// AppDelegate.m
//初始化
ViewController *vc = [[ViewController alloc]init];
UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = nvc;
// ViewController.m
//头文件
#import "Model.h"
//FMDB sqlite
#import "LoadData.h"
//重写
#import "MyTableViewCell.h"
//添加页面
#import "AddViewController.h"
//修改页面
#import "UpViewController.h"
<UITableViewDelegate,UITableViewDataSource>
{
//全局变量
UITableView *table;
NSMutableArray *marr;
}
-(void)viewWillAppear:(BOOL)animated{
//查询
marr = [[LoadData sharlLoadData]Marr];
//刷新
[table reloadData ];
}
//===========================
self.title = @"全部用户";
//表格初始化
table = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
//协议
table.delegate = self;
table.dataSource = self;
//添加到视图
[self.view addSubview:table];
//定义导航右边添加按钮
UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"+" style:UIBarButtonItemStylePlain target:self action:@selector(click)];
//添加到导航调试
self.navigationItem.rightBarButtonItem = right;
//=========================
-(void)click{
//初始化
AddViewController *add = [AddViewController new];
//跳转
[self.navigationController pushViewController:add animated:YES];
}
//行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return marr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//初始化
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];
//复用池
if (!cell) {
//初始化‘
cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];
}
//初始化
Model *mm = marr[indexPath.row];
//添加到表格上
cell.fw.text = mm.fw;
cell.zj.text = mm.zj;
cell.fh.text = mm.fh;
cell.xs.text = mm.xs;
cell.bz.text = mm.bz;
//返回值
return cell;
}
//删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//添加
Model *mm = marr[ indexPath.row];
//删除
[[LoadData sharlLoadData]deleteharlLoadData:mm];
[marr removeObjectAtIndex:indexPath.row];
//刷新
[table reloadData];
}
//跳转
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//初始化
UpViewController *up = [UpViewController new];
//添加
up.mm = marr[indexPath.row];
//修改
[[LoadData sharlLoadData]UPsharlLoadData:up.mm];
//跳转
[self.navigationController pushViewController:up animated:YES];
}
//// AddViewController.m
//头文件
#import "Model.h"
#import "LoadData.h"
@property (weak, nonatomic) IBOutlet UITextField *fw;
@property (weak, nonatomic) IBOutlet UITextField *zj;
@property (weak, nonatomic) IBOutlet UITextField *hx;
//=======括号外
- (IBAction)sss:(id)sender {
//初始化
Model *mm = [Model new];
//链接
mm.fw = self.fw.text;
mm.zj = self.zj.text;
mm.fh = self.hx.text;
//添加
[[LoadData sharlLoadData]AddsharlLoadData:mm];
//跳转
[self.navigationController popViewControllerAnimated:YES];
}
// UpViewController.m
#import "LoadData.h"
@property (weak, nonatomic) IBOutlet UITextField *fw;
@property (weak, nonatomic) IBOutlet UITextField *zj;
@property (weak, nonatomic) IBOutlet UITextField *hx;
//=================
//将数据添加到修改页面
self.fw.text = self.mm.fw;
self.zj.text = self.mm.zj;
self.hx.text = self.mm.fh;
//===================
- (IBAction)sss:(id)sender {
//初始化
Model *mm = self.mm;
//链接
mm.fw = self.fw.text;
mm.zj = self.zj.text;
mm.fh = self.hx.text;
//添加
[[LoadData sharlLoadData]UPsharlLoadData:mm];
//跳转
[self.navigationController popViewControllerAnimated:YES];
}
// MyTableViewCell.h表格重写
继承UITableViewCell
@property (nonatomic , strong) UILabel *fw;
@property (nonatomic , strong) UILabel *zj;
@property (nonatomic , strong) UILabel *fh;
@property (nonatomic , strong) UILabel *xs;
@property (nonatomic , strong) UILabel *bz;
// MyTableViewCell.m
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
//判断
if ([super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
//添加到
[self.contentView addSubview:self.fw];
[self.contentView addSubview:self.zj];
[self.contentView addSubview:self.fh];
}
//返回值
return self;
}
//懒加载
-(UILabel *)fw{
//判断
if (!_fw) {
//初始化
_fw = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 120, 44)];
}
//返回值
return _fw;
}
//懒加载
-(UILabel *)zj{
//判断
if (!_zj) {
//初始化
_zj = [[UILabel alloc]initWithFrame:CGRectMake(140, 5, 80, 44)];
}
//返回值
return _zj;
}
//懒加载
-(UILabel *)fh{
//判断
if (!_fh) {
//初始化
_fh = [[UILabel alloc]initWithFrame:CGRectMake(240, 5, 80, 44)];
}
//返回值
return _fh;
}
// Model.h继承nsobject
//定义属性
@property (nonatomic , strong) NSString *fw;
@property (nonatomic , strong) NSString *zj;
@property (nonatomic , strong) NSString *fh;
@property (nonatomic , strong) NSString *xs;
@property (nonatomic , strong) NSString *bz;
//记得nstager assign id
@property (nonatomic , assign) NSInteger ID;
//
// LoadData.h
#import "Model.h"
#import "FMDatabase.h"
@interface LoadData : NSObject
//单列类
+(instancetype)sharlLoadData;
//添加元素
-(void)AddsharlLoadData:(Model *)model;
//查询
-(NSMutableArray *)Marr;
//删除元素
-(void)deleteharlLoadData:(Model *)model;
//修改元素
-(void)UPsharlLoadData:(Model *)model;
//
// LoadData.m
static LoadData *ld = nil;
static FMDatabase *fate;
//单列类
+(instancetype)sharlLoadData{
//静态
static dispatch_once_t oneet;
//初始化
dispatch_once(&oneet, ^{
ld = [[LoadData alloc]init];
//定义初始化
[ld initA];
});
//返回值
return ld;
}
//初始化
+(instancetype)allocWithZone:(struct _NSZone *)zone{
if (!ld)
{
//初始化
ld = [super allocWithZone:zone];
}
return ld;
}
//浅复制
-(id)copy
{
return self;
}
//深复制
-(id)mutableCopy
{
return self;
}
//初始化数据库
-(void)initA{
//创建沙盒
NSString *Ste = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
//定义文件名
NSString *path = [Ste stringByAppendingPathComponent:@"HousingInfo.sqlite"];
//初始化
fate = [[FMDatabase alloc]initWithPath:path];
//判断
if ([fate open])
{
//初始化
[fate executeUpdate:@"create table class (ID integer primary key, fw text, zj text , fh text , xs text , bz text)"];
[fate close];
NSLog(@"成功");
}
else
{
NSLog(@"失败");
}
}
//添加元素
-(void)AddsharlLoadData:(Model *)model
{
//开始
[fate open];
//初始化
NSString *str = [NSString stringWithFormat:@"insert into class values (null , '%@','%@','%@','%@','%@')",model.fw,model.zj,model.fh,model.xs,model.bz];
//BOOL值接受
BOOL ii = [fate executeUpdate:str];
//判断
if (ii)
{
NSLog(@"成功");
}
else
{
NSLog(@"失败");
}
//关闭
[fate close];
}
//查询
-(NSMutableArray *)Marr{
//初始化
NSMutableArray *marr = [NSMutableArray new];
//开始
[fate open];
//初始化
FMResultSet *Set = [[FMResultSet alloc]init];
//使用set接受
Set = [fate executeQuery:@"select * from class"];
//判断
while ([Set next]) {
//初始化
Model *mm = [Model new];
//链接
mm.fw = [Set stringForColumn:@"fw"];
mm.zj = [Set stringForColumn:@"zj"];
mm.fh = [Set stringForColumn:@"fh"];
mm.bz = [Set stringForColumn:@"bz"];
mm.xs = [Set stringForColumn:@"xs"];
mm.ID = [Set intForColumn:@"ID"];
//添加到数组
[marr addObject:mm];
}
//关闭
[fate close];
//返回值
return marr;
}
//删除元素
-(void)deleteharlLoadData:(Model *)model{
//开始
[fate open];
//初始化
NSString *str = [NSString stringWithFormat:@"delete from class where ID = '%ld' ",model.ID];
//BOOL值接受
BOOL ii = [fate executeUpdate:str];
//判断
if (ii)
{
NSLog(@"成功");
}
else
{
NSLog(@"失败");
}
//关闭
[fate close];
}
//修改元素
-(void)UPsharlLoadData:(Model *)model{
//开始
[fate open];
//初始化
NSString *str = [NSString stringWithFormat:@"update class set fw = '%@',zj = '%@',fh = '%@',xs = '%@',bz = '%@' where ID = '%ld'",model.fw,model.zj,model.fh,model.xs,model.bz,model.ID];
//BOOL值接受
BOOL ii = [fate executeUpdate:str];
//判断
if (ii)
{
NSLog(@"成功");
}else{
NSLog(@"失败");
}
//关闭
[fate close];
}