CoreData

作者: Wang99 | 来源:发表于2017-12-20 14:13 被阅读0次

DataBase.h

#import <Foundation/Foundation.h>
#import "Entity+CoreDataClass.h"
#import "AppDelegate.h"

@interface DataBase : NSObject
{
    AppDelegate *app;
}
// 单例方法
+ (instancetype)showData;

// 添加数据
- (void)addData:(NSDictionary *)dic;

// 删除数据
- (void)deleteData:(Entity *)theData;

// 修改数据
- (void)changeData;

// 查询数据
- (NSMutableArray *)showAllarray;

@end

.m

#import "DataBase.h"
// 创建静态变量
static DataBase *theDatabase;

@implementation DataBase

// 单例方法
+ (instancetype)showData
{
    if (!theDatabase)
    {
        theDatabase = [[DataBase alloc]init];
    }
    return theDatabase;
}

// 添加数据
- (void)addData:(NSDictionary *)dic
{
    app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    
    // 创建实体类保存数据
    Entity *person = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:app.persistentContainer.viewContext];
    person.name = dic[@"name"];
    person.age = dic[@"age"];
    // 保存数据方法
    [app saveContext];
}

// 删除数据
- (void)deleteData:(Entity *)theData
{
    app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    // 调用删除方法进行删除
    [app.persistentContainer.viewContext deleteObject:theData];
    // 保存数据方法
    [app saveContext];
}

// 修改数据
- (void)changeData
{
    app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    
    [app saveContext]; 
}

// 查询数据
- (NSMutableArray *)showAllarray
{
    app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    
    NSFetchRequest *requst = [[NSFetchRequest alloc]init];
    
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entity" inManagedObjectContext:app.persistentContainer.viewContext];
    
    [requst setEntity:entity];
    
    NSArray *arr = [app.persistentContainer.viewContext executeFetchRequest:requst error:nil];
    
    return [arr mutableCopy];
}


@end

.h

#import <UIKit/UIKit.h>

@interface ClassView : UIView

@property (nonatomic,strong)UITextField *nameTf,*ageTf;

@end

.m

#import "ClassView.h"

@implementation ClassView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self addSubview:self.nameTf];
        [self addSubview:self.ageTf];
    }
    return self;
}

- (UITextField *)nameTf
{
    if (!_nameTf)
    {
        _nameTf = [[UITextField alloc]initWithFrame:CGRectMake(80, 80, 210, 44)];
        _nameTf.backgroundColor = [UIColor lightGrayColor];
        _nameTf.placeholder = @"请输入你的名字";
    }
    return _nameTf;
}

- (UITextField *)ageTf
{
    if (!_ageTf)
    {
        _ageTf = [[UITextField alloc]initWithFrame:CGRectMake(80, 140, 210, 44)];
        _ageTf.backgroundColor = [UIColor lightGrayColor];
        _ageTf.placeholder = @"请输入你的年龄";
    }
    return _ageTf;
}



@end

.h

#import <UIKit/UIKit.h>
#import "Entity+CoreDataClass.h"

@interface SecViewController : UIViewController

@property(nonatomic,strong)Entity *theEntity;

@end

.m

#import "DataBase.h"
#import "ClassView.h"

@interface SecViewController ()
{
    ClassView *theClassView;
}
@end

@implementation SecViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"数据变换";
    
    theClassView = [[ClassView alloc]initWithFrame:self.view.frame];
    
    theClassView.backgroundColor = [UIColor whiteColor];
    
    self.view = theClassView;
    
    theClassView.nameTf.text = self.theEntity.name;
    theClassView.ageTf.text = self.theEntity.age;
    
    if (theClassView.nameTf.text.length <= 0 )
    {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];
    }
    else
    {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(edit)];
    }
}

- (void)save
{
    // 将文本框中输入的数据放到字典当中
    NSDictionary *dic = @{@"name":theClassView.nameTf.text,@"age":theClassView.ageTf.text};
    // 添加数据
    [[DataBase showData]addData:dic];
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)edit
{
    self.theEntity.name = theClassView.nameTf.text;
    self.theEntity.age = theClassView.ageTf.text;
    
    [[DataBase showData]changeData];
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


@end
#import "ViewController.h"
#import "DataBase.h"
#import "SecViewController.h"
#import "Entity+CoreDataClass.h"

@interface ViewController ()

@property (nonatomic,strong)NSArray *dataArr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(click)];
}

- (void)click
{
    SecViewController *sec = [SecViewController new];
    
    [self.navigationController pushViewController:sec animated:YES];
}
// 视图的生命周期
- (void)viewWillAppear:(BOOL)animated
{
    // 查询数据将 DataBase 中的数据赋给当前类当中的数组
    self.dataArr = [[DataBase showData]showAllarray];
    // 刷新表格
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];
    if (!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];
    }
    // 将当前类中的数组保存的数据赋给实体对象
    Entity *enty = self.dataArr[indexPath.row];
    cell.textLabel.text = enty.name;
    cell.detailTextLabel.text = enty.age;
    return cell;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    Entity *enty = self.dataArr[indexPath.row];
    // 删除数据
    [[DataBase showData]deleteData:enty];
    // 再重新获取数据
    self.dataArr = [[DataBase showData]showAllarray];
    
    [self.tableView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecViewController *sec = [SecViewController new];
    // 传值(传给下一个控制器中的实体对象)
    sec.theEntity = self.dataArr[indexPath.row];
    
    [self.navigationController pushViewController:sec animated:YES];
}

@end

相关文章

网友评论

      本文标题:CoreData

      本文链接:https://www.haomeiwen.com/subject/dzsgwxtx.html