CoreData单表

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值