首先第一步 创建 CoreData 工程需要:1.
点击这个在里面添加实体 。
2.
3.
这里需要几个数据 就添加几个字符串
4.
5.切记这里一定要打对勾
6.切记这里要把对号去掉然后创建成功
下面是代码部分 :
运用MVC设计模式 创建Model类继承NSobject 在DataBase.h中:
//
// DataBase.h
// 1607ECoreData
//
// Created by mac on 2018/8/13.
// Copyright © 2018年 com.bawei.www. All rights reserved.
//
#import <Foundation/Foundation.h>
#import “AppDelegate.h”
#import “Entity+CoreDataClass.h”
@interface DataBase : NSObject
{
AppDelegate *app;
}
//单利方法
+(instancetype)initData;
//添加数据
-(void)addData:(NSDictionary *)dic;
//删除数据
-(void)deleteData:(Entity *)data;
//修改
-(void)upData;
//查询
-(NSMutableArray *)array;
@end
在DataBase.m 中:
//
// DataBase.m
// 1607ECoreData
//
// Created by mac on 2018/8/13.
// Copyright © 2018年 com.bawei.www. All rights reserved.
//
#import “DataBase.h”
//静态变量
static DataBase *da;
@implementation DataBase
//单利方法
-
(instancetype)initData{
if (!da) {
da = [[DataBase alloc] init];}
return da;
}
//添加数据
-
(void)addData:(NSDictionary *)dic{
//创建appdelegate对象
app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//创建实体对象 NSEntityDescription(实体描叙对象)
Entity *cla = [NSEntityDescription insertNewObjectForEntityForName:@“Entity” inManagedObjectContext:app.persistentContainer.viewContext];
//添加数据
cla.name = dic[@“name”];
cla.age = dic[@“age”];
cla.sex = dic[@“sex”];//保存数据
[app saveContext];
}
//删除数据
-
(void)deleteData:(Entity *)data{
//创建appdelegate对象
app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//删除
[app.persistentContainer.viewContext deleteObject:data];
//保存数据
[app saveContext];
}
//修改数据
-
(void)upData{
//创建appdelegate对象
app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//保存数据
[app saveContext];
}
//查询数据
-
(NSMutableArray *)array{
//创建appdelegate对象
app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//创建请求数据对象
NSFetchRequest *requst = [[NSFetchRequest alloc] init];
//创建实体描述类
NSEntityDescription *ent = [NSEntityDescription entityForName:@“Entity” inManagedObjectContext:app.persistentContainer.viewContext];
//获取数据
[requst setEntity:ent];
//从实体中获取数据
NSArray *arr = [app.persistentContainer.viewContext executeFetchRequest:requst error:nil];return [arr mutableCopy];
}
@end
下面是View文件夹里面的东西 :创建类继承UIview
#import <UIKit/UIKit.h>
@interface ClassView : UIView
@property(nonatomic,strong)UITextField *nameTf,*ageTf,*sexTf;
@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];
}
return self;
}
//懒加载
-(UITextField *)nameTf{
if (!_nameTf) {
_nameTf = [[UITextField alloc]initWithFrame:CGRectMake(0, 90, self.frame.size.width, 44)];
_nameTf.placeholder = @"please you name";
_nameTf.borderStyle = UITextBorderStyleRoundedRect;
_nameTf.textAlignment = NSTextAlignmentCenter;
}
return _nameTf;
}
-(UITextField *)ageTf{
if (!_ageTf) {
_ageTf = [[UITextField alloc]initWithFrame:CGRectMake(0, 145, self.frame.size.width, 44)];
_ageTf.placeholder = @"please you age";
_ageTf.borderStyle = UITextBorderStyleRoundedRect;
_ageTf.textAlignment = NSTextAlignmentCenter;
}
return _ageTf;
}
-(UITextField *)sexTf{
if (!_sexTf) {
_sexTf = [[UITextField alloc]initWithFrame:CGRectMake(0, 190, self.frame.size.width, 44)];
_sexTf.placeholder = @"please you sex";
_sexTf.borderStyle = UITextBorderStyleRoundedRect;
_sexTf.textAlignment = NSTextAlignmentCenter;
}
return _sexTf;
}
@end
首先要在Appdelegate.m里面把VC设置成跟控制器
下面是Controller里面的东西
首先把VC.h里面的继承改成 UITableViewController
其次是VC.m里面的东西:
//
// ViewController.m
// 1607ECoreData
//
// Created by mac on 2018/8/13.
// Copyright © 2018年 com.bawei.www. All rights reserved.
//
#import “ViewController.h”
#import “EditViewController.h”
#import “DataBase.h”
#import “Entity+CoreDataClass.h”
@interface ViewController ()
{
NSMutableArray *arr;
}
@end
@implementation ViewController
-
(void)viewDidLoad {
[super viewDidLoad];self.title = @“CoreData单表”;
//设置行高
self.tableView.rowHeight = 100;
//创建数组
arr = [NSMutableArray array];
//设置右按钮
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(click)];
}
//点击右侧按钮方法
-(void)click{
//跳转到第二个控制器
EditViewController *editVc = [[EditViewController alloc]init];
//左右滑动
[self.navigationController pushViewController:editVc animated:YES];
}
//视图即将显示
-
(void)viewWillAppear:(BOOL)animated{
arr = [[DataBase initData] array];
//刷新表格
[self.tableView reloadData];
}
#pragma mark UITableViewDataSource
-
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return arr.count;
}
//设置cell内容
-
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@“cell”];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@“cell”];}
//创建实体对象
Entity *cla = arr[indexPath.row];
//设置cell内容
cell.textLabel.text = [NSString stringWithFormat:@"%@\n%@\n%@",cla.name,cla.age,cla.sex];
cell.textLabel.numberOfLines = 0;
return cell;
}
//点击单元格方法 修改数据
-
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
EditViewController *edit = [[EditViewController alloc] init];
edit.cla = arr[indexPath.row];
[self.navigationController pushViewController:edit animated:YES];
}
//处于编辑状态方法
-
(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//创建实体
Entity *cla = arr[indexPath.row];
//调用删除数据方法
[[DataBase initData]deleteData:cla];
//重新获取数据
arr = [[DataBase initData]array];
//刷新表格
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
下面是第二个控制器.h 里面的东西:
//
// EditViewController.h
// 1607ECoreData
//
// Created by mac on 2018/8/13.
// Copyright © 2018年 com.bawei.www. All rights reserved.
//
#import <UIKit/UIKit.h>
#import “Entity+CoreDataClass.h”
@interface EditViewController : UIViewController
@property(nonatomic,strong)Entity *cla;
@end
第二个控制器.m里面里面的东西:
#import “EditViewController.h”
#import “ClassView.h”
#import “DataBase.h”
@interface EditViewController ()
{
ClassView *theView;
}
@end
@implementation EditViewController
-
(void)viewDidLoad {
[super viewDidLoad];theView = [[ClassView alloc] initWithFrame:self.view.frame];
theView.backgroundColor = [UIColor whiteColor];
self.view = theView;theView.nameTf.text = self.cla.name;
theView.ageTf.text = self.cla.age;theView.sexTf.text = self.cla.sex;
if (self.cla == nil) {
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)];
}
}
//保存数据方法
-(void)save{
NSDictionary *dic = @{@"name":theView.nameTf.text,@"age":theView.ageTf.text,@"sex":theView.sexTf.text};
//调用保存数据方法
[[DataBase initData] addData:dic];
//返回上一级
[self.navigationController popViewControllerAnimated:YES];
}
//修改数据
-(void)edit{
self.cla.name = theView.nameTf.text;
self.cla.age = theView.ageTf.text;
self.cla.sex = theView.sexTf.text;
//调用修改方法
[[DataBase initData] upData];
//返回上一级
[self.navigationController popViewControllerAnimated:YES];
}