//联系人:石虎QQ: 1224614774昵称:嗡嘛呢叭咪哄
/**
注意点: 1.看 GIF 效果图.
2.看连线视图的效果图.
3.看实现代码(直接复制实现效果).
4.看实现代码MVC架构
5.详细鲜明代码 链接: http://blog.csdn.net/shihuboke/article/details/76832759
*/
一、GIF 效果图:
二、连线视图的效果图:
图1:
图2:
图3:
三、实现代码:
=======
================================
=======
1.AppDelegate.m 中的代码
#import"AppDelegate.h"
#import"ViewController.h"//主控制器
@interfaceAppDelegate()
@end
@implementationAppDelegate
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
//初始化
ViewController*vc = [[ViewControlleralloc]init];
//导航控制器
UINavigationController*nvc = [[UINavigationControlleralloc]initWithRootViewController:vc];
//添加图
self.window.rootViewController= nvc;
returnYES;
}
================================
=======
2.导入第三方数据库FMDB 框架包
下载地址:https://github.com/ccgus/fmdb
注意:要导入 libsqlite3.tbd 包 (详情请看上面图3)
================================
=======
3.在 Other 模块中添加业务处理层处理 SQLite 语句
注意:如果现在拷贝到项目会报错-->原因是其他 MVC 模块中的类没有实现
控制器1 .h:
// Created by石虎on 2017/7/6.
// Copyright © 2017年shihu. All rights reserved.
//
#import
#import"Model.h"
#import"FMDatabase.h"
@interfaceLoadData :NSObject
//单列类
+(instancetype)sharlLoadData;
//添加元素
-(void)AddsharlLoadData:(Model*)model;
//查询
-(NSMutableArray*)Marr;
//删除元素
-(void)deleteharlLoadData:(Model*)model;
//修改元素
-(void)UPsharlLoadData:(Model*)model;
@end
控制器1 .M:
// Created by石虎on 2017/7/6.
// Copyright © 2017年shihu. All rights reserved.
//
#import"LoadData.h"
staticLoadData*ld =nil;
staticFMDatabase*fate;
@implementationLoadData
//单列类
+(instancetype)sharlLoadData{
//静态
staticdispatch_once_toneet;
//初始化
dispatch_once(&oneet, ^{
ld= [[LoadDataalloc]init];
//定义初始化
[ldinitA];
});
//返回值
returnld;
}
//初始化
+(instancetype)allocWithZone:(struct_NSZone*)zone{
if(!ld) {
//初始化
ld= [superallocWithZone:zone];
}
returnld;
}
//浅复制
-(id)copy{
returnself;
}
//深复制
-(id)mutableCopy{
returnself;
}
//初始化数据库
-(void)initA{
//创建沙盒
NSString*Ste = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];
//定义文件名
NSString*path = [StestringByAppendingPathComponent:@"HousingInfo.sqlite"];
//初始化
fate= [[FMDatabasealloc]initWithPath:path];
//判断
if([fateopen]) {
//初始化
[fateexecuteUpdate:@"create table class (ID integer primary key, fieldMail text, fieldAddress text , fieldConsignee text , fieldPhone text , fieldNote text)"];
[fateclose];
NSLog(@"成功");
}else{
NSLog(@"失败");
}
}
//添加元素
-(void)AddsharlLoadData:(Model*)model{
//开始
[fateopen];
//初始化
NSString*str = [NSStringstringWithFormat:@"insert into class values (null , '%@','%@','%@','%@','%@')",model.fieldMail,model.fieldAddress,model.fieldConsignee,model.fieldPhone,model.fieldNote];
//BOOL值接受
BOOLii = [fateexecuteUpdate:str];
//判断
if(ii) {
NSLog(@"成功");
}else{
NSLog(@"失败");
}
//关闭
[fateclose];
}
//查询
-(NSMutableArray*)Marr{
//初始化
NSMutableArray*marr = [NSMutableArraynew];
//开始
[fateopen];
//初始化
FMResultSet*Set = [[FMResultSetalloc]init];
//使用set接受
Set = [fateexecuteQuery:@"select * from class"];
//判断
while([Setnext]) {
//初始化
Model*mm = [Modelnew];
//链接
mm.fieldMail= [SetstringForColumn:@"fieldMail"];
mm.fieldAddress = [Set stringForColumn:@"fieldAddress"];
mm.fieldConsignee = [Set stringForColumn:@"fieldConsignee"];
mm.fieldPhone = [Set stringForColumn:@"fieldPhone"];
mm.fieldNote = [Set stringForColumn:@"fieldNote"];
mm.ID = [Set intForColumn:@"ID"];
//添加到数组
[marr addObject:mm];
}
//关闭
[fate close];
//返回值
returnmarr;
}
//删除元素
-(void)deleteharlLoadData:(Model *)model{
//开始
[fate open];
//初始化
NSString *str = [NSString stringWithFormat:@"delete from class where ID = '%ld' ",model.ID];
//BOOL值接受
BOOLii = [fate executeUpdate:str];
//判断
if(ii) {
NSLog(@"成功");
}else{
NSLog(@"失败");
}
//关闭
[fate close];
}
//修改元素
-(void)UPsharlLoadData:(Model *)model{
//开始
[fate open];
//初始化
NSString *str = [NSString stringWithFormat:@"update class set fieldMail = '%@',fieldAddress = '%@',fieldConsignee = '%@',fieldPhone = '%@',fieldNote = '%@' where ID = '%ld'",model.fieldMail,model.fieldAddress,model.fieldConsignee,model.fieldPhone,model.fieldNote,model.ID]
//BOOL值接受
BOOLii = [fate executeUpdate:str];
//判断
if(ii) {
NSLog(@"成功");
}else{
NSLog(@"失败");
}
//关闭
[fate close];
}
@end
================ ================
=======
4.MVC-->C模块
控制器1 -主控制器 .m:
// Created by石虎on 2017/7/6.
// Copyright © 2017年shihu. All rights reserved.
//
#import"ViewController.h"
#import"LoadData.h"//业务处理SQLite
#import"Model.h"//保存数据
#import"MyTableViewCell.h"//主控制器cell
#import"UpViewController.h"//修改信息控制器
#import"AddViewController.h"//添加信息控制器
@interfaceViewController(){
UITableView*tabele;
NSMutableArray*marr;
}
@end
@implementationViewController
//将要显示
-(void)viewWillAppear:(BOOL)animated{
//查询
marr= [[LoadDatasharlLoadData]Marr];
//刷新
[tabelereloadData];
}
- (void)viewDidLoad {
[superviewDidLoad];
//定义标题
self.title=@"信息查询系统";
//初始化
tabele= [[UITableViewalloc]initWithFrame:self.view.framestyle:UITableViewStylePlain];
//添加协议
tabele.delegate=self;
tabele.dataSource=self;
//添加到试图上
[self.viewaddSubview:tabele];
//定义按钮
UIBarButtonItem*right = [[UIBarButtonItemalloc]initWithTitle:@"添加"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(click)];
//添加到导航调试
self.navigationItem.rightBarButtonItem= right;
}
-(void)click{
//初始化
AddViewController*add = [AddViewControllernew];
//跳转
[self.navigationControllerpushViewController:addanimated:YES];
}
//行数
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
returnmarr.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
//初始化
MyTableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:@""];
//复用池
if(!cell) {
//初始化‘
cell = [[MyTableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:@""];
}
//初始化
Model*mm =marr[indexPath.row];
//添加到表格上
cell.fieldMail.text= mm.fieldMail;
cell.fieldAddress.text= mm.fieldAddress;
cell.fieldConsignee.text= mm.fieldConsignee;
cell.fieldPhone.text= mm.fieldPhone;
cell.fieldNote.text= mm.fieldNote;
//返回值
returncell;
}
//删除
-(void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{
//添加
Model*mm =marr[ indexPath.row];
//删除
[[LoadDatasharlLoadData]deleteharlLoadData:mm];
[marr removeObjectAtIndex:indexPath.row];
//刷新
[tabele 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];
}
@end
================================
控制器2--添加信息控制器 .m:
// Created by石虎on 2017/7/6.
// Copyright © 2017年shihu. All rights reserved.
//
#import"AddViewController.h"
#import"Model.h"
#import"LoadData.h"
@interfaceAddViewController()
//邮件
@property(strong,nonatomic)IBOutletUITextField*fieldMail;
//收货地址
@property(strong,nonatomic)IBOutletUITextField*fieldAddress;
//收货人
@property(strong,nonatomic)IBOutletUITextField*fieldConsignee;
//电话
@property(strong,nonatomic)IBOutletUITextField*fieldPhone;
//备注
@property(strong,nonatomic)IBOutletUITextField*fieldNote;
//点击添加信息
- (IBAction)sss:(id)sender;
@end
@implementationAddViewController
- (void)viewDidLoad {
[superviewDidLoad];
}
- (IBAction)sss:(id)sender {
//初始化
Model*mm = [Modelnew];
//链接
mm.fieldMail=self.fieldMail.text;
mm.fieldAddress=self.fieldAddress.text;
mm.fieldConsignee=self.fieldConsignee.text;
mm.fieldPhone=self.fieldPhone.text;
mm.fieldNote=self.fieldNote.text;
//添加到数据库
[[LoadDatasharlLoadData]AddsharlLoadData:mm];
//跳转
[self.navigationControllerpopViewControllerAnimated:YES];
}
@end
================================
控制器3--修改信息控制器 .h:
// Created by石虎on 2017/7/6.
// Copyright © 2017年shihu. All rights reserved.
//
#import
@classModel;
@interfaceUpViewController :UIViewController
//定义属性
@property(nonatomic,strong)Model*mm;
@end
控制器3--修改信息控制器 .m:
// Created by石虎on 2017/7/6.
// Copyright © 2017年shihu. All rights reserved.
//
#import"UpViewController.h"
#import"LoadData.h"
#import"Model.h"
@interfaceUpViewController()
//邮件
@property(strong,nonatomic)IBOutletUITextField*fieldMail;
//收货地址
@property(strong,nonatomic)IBOutletUITextField*fieldAddress;
//收货人
@property(strong,nonatomic)IBOutletUITextField*fieldConsignee;
//电话
@property(strong,nonatomic)IBOutletUITextField*fieldPhone;
//备注
@property(strong,nonatomic)IBOutletUITextField*fieldNote;
//点击修改信息
- (IBAction)ssss:(id)sender;
@end
@implementationUpViewController
- (void)viewDidLoad {
[superviewDidLoad];
//将数据添加到修改页面
self.fieldMail.text =self.mm.fieldMail;
self.fieldAddress.text =self.mm.fieldAddress;
self.fieldConsignee.text =self.mm.fieldConsignee;
self.fieldPhone.text =self.mm.fieldPhone;
self.fieldNote.text =self.mm.fieldNote;
}
- (IBAction)ssss:(id)sender {
//初始化
Model *mm =self.mm;
//链接
mm.fieldMail =self.fieldMail.text;
mm.fieldAddress =self.fieldAddress.text;
mm.fieldConsignee =self.fieldConsignee.text;
mm.fieldPhone =self.fieldPhone.text;
mm.fieldNote =self.fieldNote.text;
//添加
[[LoadData sharlLoadData]UPsharlLoadData:mm];
//跳转
[self.navigationController popViewControllerAnimated:YES];
}
@end
================================
=======
5.MVC-->V模块
控制器1-- .h:
// Created by石虎on 2017/7/6.
// Copyright © 2017年shihu. All rights reserved.
//
#import
@interfaceMyTableViewCell :UITableViewCell
//邮件
@property(nonatomic,strong)UILabel*fieldMail;
//收货地址
@property(nonatomic,strong)UILabel*fieldAddress;
//收货人
@property(nonatomic,strong)UILabel*fieldConsignee;
//电话
@property(nonatomic,strong)UILabel*fieldPhone;
//备注
@property(nonatomic,strong)UILabel*fieldNote;
@end
控制器1-- .m:
// Created by石虎on 2017/7/6.
// Copyright © 2017年shihu. All rights reserved.
//
#import"MyTableViewCell.h"
@implementationMyTableViewCell
//重写父类方法
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier{
//判断
if([superinitWithStyle:stylereuseIdentifier:reuseIdentifier]) {
//添加到
[self.contentViewaddSubview:self.fieldMail];
[self.contentViewaddSubview:self.fieldAddress];
[self.contentViewaddSubview:self.fieldConsignee];
[self.contentViewaddSubview:self.fieldPhone];
[self.contentViewaddSubview:self.fieldNote];
}
//返回值
returnself;
}
//懒加载-//邮件
-(UILabel*)fieldMail{
//判断
if(!_fieldMail) {
//初始化
_fieldMail= [[UILabelalloc]initWithFrame:CGRectMake(5,5,80,44)];
}
//返回值
return_fieldMail;
}
//懒加载-//收货地址
-(UILabel*)fieldAddress{
//判断
if(!_fieldAddress) {
//初始化
_fieldAddress= [[UILabelalloc]initWithFrame:CGRectMake(80,5,80,44)];
}
//返回值
return_fieldAddress;
}
//懒加载=//收货人
-(UILabel*)fieldConsignee{
//判断
if(!_fieldConsignee) {
//初始化
_fieldConsignee= [[UILabelalloc]initWithFrame:CGRectMake(155,5,80,44)];
}
//返回值
return_fieldConsignee;
}
//懒加载--//电话
-(UILabel*)fieldPhone{
//判断
if(!_fieldPhone) {
//初始化
_fieldPhone = [[UILabel alloc]initWithFrame:CGRectMake(220,5,80,44)];
}
//返回值
return_fieldPhone;
}
//懒加载
-(UILabel *)fieldNote{
//判断
if(!_fieldNote) {
//初始化
_fieldNote = [[UILabel alloc]initWithFrame:CGRectMake(305,5,80,44)];
}
//返回值
return_fieldNote;
}
- (void)awakeFromNib {
[superawakeFromNib];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[supersetSelected:selected animated:animated];
}
@end
================================
=======
6.MVC-->M模块
控制器1-- .h:
// Created by石虎on 2017/7/6.
// Copyright © 2017年shihu. All rights reserved.
//
#import
@interfaceModel :NSObject
//定义属性
//邮箱
@property(nonatomic,copy)NSString*fieldMail;
//收货地址
@property(nonatomic,copy)NSString*fieldAddress;
//收货人
@property(nonatomic,copy)NSString*fieldConsignee;
//电话
@property(nonatomic,copy)NSString*fieldPhone;
//备注
@property(nonatomic,copy)NSString*fieldNote;
//
@property(nonatomic,assign)NSIntegerID;
@end
================================
控制器1-- .m:
// Created by石虎on 2017/7/6.
// Copyright © 2017年shihu. All rights reserved.
//
#import"Model.h"
@implementationModel
@end
================================
=======
================================
=======
谢谢!!!