CoreData单表
#import <Foundation/Foundation.h>
#import “Student+CoreDataClass.h”
@interface DataManager : NSObject
+(DataManager *)shareManager;
-(void)insert:(NSDictionary *)dic;
-(void)update:(Student *)student;
-(void)deleteData:(Student *)student;
-(NSArray *)select;
#import “DataManager.h”
#import “AppDelegate.h”
@interface DataManager(){
//声明一个AppDelegate对象属性,调用里面的 被管理对象上下文 保存方法
AppDelegate * myDelegate;
}
@end
@implementation DataManager
+(DataManager *)shareManager{
static DataManager * manager;
if (!manager) {
manager = [[DataManager alloc]init];
}
return manager;
}
-(void)insert:(NSDictionary *)dic{
//coreData 数据的插入
myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//创建实体描述对象
NSEntityDescription * description = [NSEntityDescription entityForName:@“Student” inManagedObjectContext:myDelegate.persistentContainer.viewContext];
//创建一个模型对象
Student * student = [[Student alloc]initWithEntity:description insertIntoManagedObjectContext:myDelegate.persistentContainer.viewContext];
student.name = dic[@“name”];
student.age = [dic[@“age”]integerValue];
student.stuID = [dic[@“stuID”] integerValue];
//对数据管理器的更改进行永久保存
[myDelegate saveContext];
}
-(void)update:(Student *)student{
myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[myDelegate saveContext];
}
-(NSArray *)select{
myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//查询数据
//1.创建查询请求
NSFetchRequest * request = [[NSFetchRequest alloc]initWithEntityName:@“Student”];
//2,执行这个查询请求
NSError * error = nil;
NSArray * result =[myDelegate.persistentContainer.viewContext executeFetchRequest:request error:&error];
return result;
}
-(void)deleteData:(Student *)student{
myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[myDelegate.persistentContainer.viewContext deleteObject:student];
[myDelegate saveContext];
}
@end
#import <UIKit/UIKit.h>
#import “Student+CoreDataClass.h”
@interface TwoVC : UIViewController
@property(nonatomic,assign) NSInteger typeID;
@property(nonatomic,strong) Student * student;
@end
#import “TwoVC.h”
#import “DataManager.h”
@interface TwoVC ()
@property (weak, nonatomic) IBOutlet UITextField *stuIDTF;
@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UITextField *ageTF;
@end
@implementation TwoVC
-
(void)viewDidLoad {
[super viewDidLoad];
[self setNav];
}
-(void)setNav{
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveAction)];if (self.typeID ==1) {
//添加
self.title = @“添加数据”;
}
else{
//修改
self.title = @“修改数据”;
self.stuIDTF.text = [NSString stringWithFormat:@"%d",_student.stuID];
self.nameTF.text = _student.name;
self.ageTF.text =[NSString stringWithFormat:@"%d",_student.age];
}
}
-(void)saveAction{
if (self.typeID ==1) {
//添加方法
[[DataManager shareManager]insert:@{
@“stuID”?([_stuIDTF.text integerValue]),@"name":_nameTF.text, @"age":@([_ageTF.text integerValue]) }];
}
else{
//修改方法
self.student.stuID = [_stuIDTF.text integerValue];
self.student.name = _nameTF.text;
self.student.age = [_ageTF.text integerValue];
[[DataManager shareManager] update:_student];}
[self.navigationController popViewControllerAnimated:YES];
}
@end
#import “ViewController.h”
#import “TwoVC.h”
#import “DataManager.h”
#import “Student+CoreDataClass.h”
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView * tableView;
@property(nonatomic,strong) NSMutableArray * dataSource;
@end
@implementation ViewController
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//查询数据
self.dataSource = [NSMutableArray arrayWithArray:[[DataManager shareManager]select]];
[self.tableView reloadData];
}
-
(void)viewDidLoad {
[super viewDidLoad];
[self setNav];
[self.view addSubview:self.tableView];
}
-(UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
}
_tableView.delegate = self;
_tableView.dataSource = self;
return _tableView;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@“cell”];
if (cell==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@“cell”];
}
Student * s = _dataSource[indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@“stuID = %d,name = %@,age = %d”,s.stuID,s.name,s.age];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//点击单元格跳转页面
TwoVC * two = [[TwoVC alloc]init];
two.student = _dataSource[indexPath.row];
[self.navigationController pushViewController:two animated:YES];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{//删除数据库的
[[DataManager shareManager]deleteData:_dataSource[indexPath.row]];
//删除数据源数据
[_dataSource removeObject:_dataSource[indexPath.row]];
[self.tableView reloadData];
}
-(void)setNav{
self.title = @“CoreData单表”;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction)];
}
-(void)addAction{
TwoVC * two = [[TwoVC alloc]init];
two.typeID = 1;//代表添加
[self.navigationController pushViewController:two animated:YES];
}
@end