#import "DataManager.h"
#import "AppDelegate.h"
@interfaceDataManager(){
? ? //聲明一個AppDelegate對象屬性,調(diào)用里面的被管理的對象上下文 保存方法
? ? AppDelegate * myDelegate;
}
@end
@implementation DataManager
//單例創(chuàng)建
+(DataManager *)shareManager{
? ? staticDataManager * manager;
? ? if(!manager) {
? ? ? ? manager = [[DataManager alloc]init];
? ? }
? ? returnmanager;
}
//插入
-(void)insert:(NSDictionary *)dic{
?? //CoreData數(shù)據(jù)的插入
? ? //新方法 **
? ? myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
? ? //創(chuàng)建實體描述對象
?? NSEntityDescription * descrption = [NSEntityDescription entityForName:@"Student"inManagedObjectContext:myDelegate.persistentContainer.viewContext];
? ? //創(chuàng)建一個模型對
? ? Student * student = [[Student alloc] initWithEntity:descrption insertIntoManagedObjectContext:myDelegate.persistentContainer.viewContext];
? ? student.name = dic[@"name"];
? ? student.age = [dic[@"age"]integerValue];
? ? student.sex = [dic[@"sex"]integerValue];
? ? //對數(shù)據(jù)管理器的更改進行永久保存
? ? [myDelegate saveContext];
}
//更改
-(void)updata:(Student *)student{
? ? myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
? ? [myDelegate saveContext];
}
//查找
-(NSArray *)select{
?? ? myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
? ? //查詢數(shù)據(jù)
? ? //1.創(chuàng)建查詢請求
? ? NSFetchRequest * request = [[NSFetchRequest alloc]initWithEntityName:@"Student"];
? ? //獲取地址
? ? NSError * error =nil;
? ? //2.執(zhí)行這個查詢請求
?? NSArray * result =? [myDelegate.persistentContainer.viewContext executeFetchRequest:request error:&error];
? ? returnresult;
}
//刪除
-(void)deleteData:(Student *)student{
?? ? myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
? ? [myDelegate.persistentContainer.viewContext deleteObject:student];
? ? [myDelegate saveContext];
}
@end
#import "ViewController.h"
#import "TwoVc.h"
#import "Student+CoreDataClass.h"
#import "DataManager.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,strong)NSMutableArray * dataSource;
@end
@implementation ViewController
-(void)viewWillAppear:(BOOL)animated{
? ? [superviewWillAppear:animated];
? ? // 查詢數(shù)據(jù)
? ? self.dataSource = [NSMutableArray arrayWithArray:[[DataManager shareManager]select]];
? ? [self.tableView reloadData];
}
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? [selfsetNav];
}
-(UITableView*)tableView{
? ? if (!_tableView) {
? ? ? ? _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
? ? ? ? _tableView.delegate=self;
? ? ? ? _tableView.dataSource=self;
? ? ? ? [self.viewaddSubview:_tableView];
? ? }
? ? return _tableView;
}
-(void)setNav{
? ? self.title = @"CoreData單表";
? ? self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"?" style:UIBarButtonItemStylePlain target:self action:@selector(els)];
}
-(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:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
? ? }
? ? Student *s = _dataSource[indexPath.row];
? ? cell.textLabel.text = [NSString stringWithFormat:@"sex = %d,name %@,age = %d",s.sex,s.name,s.age];
? ? returncell;
}
-(void)els{
? ? TwoVc* t = [TwoVcnew];
? ? [self.navigationController pushViewController:t animated:nil];
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
? ? //點擊單元格跳轉(zhuǎn)界面
? ? TwoVc* two = [TwoVcnew];
? ? two.student=_dataSource[indexPath.row];
? ? [self.navigationController pushViewController:two animated:nil];
}
- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{
? ? [[DataManagershareManager]deleteData:_dataSource[indexPath.row]];
? ? //
? ? [_dataSource removeObject:_dataSource[indexPath.row]];
? ? [self.tableView reloadData];
}
@end
#import
#import "Student+CoreDataClass.h"
@interface TwoVc : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *age;
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *sex;
@property(nonatomic , assign)NSInteger typeID;
@property(nonatomic , strong)Student * student;
@end
#import "TwoVc.h"
#import "DataManager.h"
@interface TwoVc ()
@end
@implementation TwoVc
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? //右按鈕
? ? self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(savetion)];
? ? if(self.typeID==1) {
? ? ? ? //添加數(shù)據(jù)
? ? ? ? self.title=@"添加數(shù)據(jù)";
? ? }else{
? ? ? ? self.title=@"修改數(shù)據(jù)";
? ? ? ? self.sex.text = [NSString stringWithFormat:@"%d",_student.sex];
? ? ? ? self.name.text = _student.name;
? ? ? ? self.age.text = [NSString stringWithFormat:@"%d",_student.age];
? ? }
}
-(void)savetion{
? ? if(self.typeID==1) {
? ? ? ? //添加
? ? ? ? self.title=@"添加數(shù)據(jù)";
? ? ? ? [[DataManagershareManager]insert:@{@"stuID ":@([_sex.textintegerValue]),@"name":_name.text,@"age":@([_age.textintegerValue])}];
? ? }else{
? ? ? ? //修改
? ? ? ? self.title=@"修改數(shù)據(jù)";
? ? ? ? self.student.sex = [_sex.text integerValue];
? ? ? ? self.student.name = _name.text;
? ? ? ? self.student.age = [_age.text integerValue];
? ? ? ? [[DataManager shareManager] updata:_student];
? ? }
? ? [self
?? ? .navigationController popViewControllerAnimated:YES];
}
@end
+ (NSFetchRequest *)fetchRequest;
@property (nullable, nonatomic, copy) NSString *name;
@property (nonatomic) int16_t age;
@property (nonatomic) int16_t sex;