coredata

2018-12-17  本文已影响0人  小美788

#import "DataManager.h"

#import "AppDelegate.h"

@interfaceDataManager(){

    //声明一个AppDelegate对象属性,调用里面的被管理的对象上下文 保存方法

    AppDelegate * myDelegate;

}

@end

@implementation DataManager

//单例创建

+(DataManager *)shareManager{

    staticDataManager * manager;

    if(!manager) {

        manager = [[DataManager alloc]init];

    }

    returnmanager;

}

//插入

-(void)insert:(NSDictionary *)dic{

   //CoreData数据的插入

    //新方法 **

    myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    //创建实体描述对象

   NSEntityDescription * descrption = [NSEntityDescription entityForName:@"Student"inManagedObjectContext:myDelegate.persistentContainer.viewContext];

    //创建一个模型对

    Student * student = [[Student alloc] initWithEntity:descrption insertIntoManagedObjectContext:myDelegate.persistentContainer.viewContext];

    student.name = dic[@"name"];

    student.age = [dic[@"age"]integerValue];

    student.sex = [dic[@"sex"]integerValue];

    //对数据管理器的更改进行永久保存

    [myDelegate saveContext];

}

//更改

-(void)updata:(Student *)student{

    myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    [myDelegate saveContext];

}

//查找

-(NSArray *)select{

     myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    //查询数据

    //1.创建查询请求

    NSFetchRequest * request = [[NSFetchRequest alloc]initWithEntityName:@"Student"];

    //获取地址

    NSError * error =nil;

    //2.执行这个查询请求

   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];

    // 查询数据

    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{

    //点击单元格跳转界面

    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) {

        //添加数据

        self.title=@"添加数据";

    }else{

        self.title=@"修改数据";

        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=@"添加数据";

        [[DataManagershareManager]insert:@{@"stuID ":@([_sex.textintegerValue]),@"name":_name.text,@"age":@([_age.textintegerValue])}];

    }else{

        //修改

        self.title=@"修改数据";

        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;

上一篇下一篇

猜你喜欢

热点阅读