CoreData

DataBase.h

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

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

// 添加數(shù)據(jù)
- (void)addData:(NSDictionary *)dic;

// 刪除數(shù)據(jù)
- (void)deleteData:(Entity *)theData;

// 修改數(shù)據(jù)
- (void)changeData;

// 查詢數(shù)據(jù)
- (NSMutableArray *)showAllarray;

@end

.m

#import "DataBase.h"
// 創(chuàng)建靜態(tài)變量
static DataBase *theDatabase;

@implementation DataBase

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

// 添加數(shù)據(jù)
- (void)addData:(NSDictionary *)dic
{
    app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    
    // 創(chuàng)建實體類保存數(shù)據(jù)
    Entity *person = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:app.persistentContainer.viewContext];
    person.name = dic[@"name"];
    person.age = dic[@"age"];
    // 保存數(shù)據(jù)方法
    [app saveContext];
}

// 刪除數(shù)據(jù)
- (void)deleteData:(Entity *)theData
{
    app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    // 調(diào)用刪除方法進行刪除
    [app.persistentContainer.viewContext deleteObject:theData];
    // 保存數(shù)據(jù)方法
    [app saveContext];
}

// 修改數(shù)據(jù)
- (void)changeData
{
    app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    
    [app saveContext]; 
}

// 查詢數(shù)據(jù)
- (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 = @"數(shù)據(jù)變換";
    
    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
{
    // 將文本框中輸入的數(shù)據(jù)放到字典當(dāng)中
    NSDictionary *dic = @{@"name":theClassView.nameTf.text,@"age":theClassView.ageTf.text};
    // 添加數(shù)據(jù)
    [[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
{
    // 查詢數(shù)據(jù)將 DataBase 中的數(shù)據(jù)賦給當(dāng)前類當(dāng)中的數(shù)組
    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:@""];
    }
    // 將當(dāng)前類中的數(shù)組保存的數(shù)據(jù)賦給實體對象
    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];
    // 刪除數(shù)據(jù)
    [[DataBase showData]deleteData:enty];
    // 再重新獲取數(shù)據(jù)
    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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市杏瞻,隨后出現(xiàn)的幾起案子能岩,更是在濱河造成了極大的恐慌浪感,老刑警劉巖更卒,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異洛口,居然都是意外死亡矫付,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進店門第焰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來买优,“玉大人,你說我怎么就攤上這事挺举∩庇” “怎么了?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵湘纵,是天一觀的道長脂崔。 經(jīng)常有香客問我,道長梧喷,這世上最難降的妖魔是什么砌左? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任脖咐,我火速辦了婚禮,結(jié)果婚禮上汇歹,老公的妹妹穿的比我還像新娘屁擅。我一直安慰自己,他們只是感情好秤朗,可當(dāng)我...
    茶點故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布煤蹭。 她就那樣靜靜地躺著,像睡著了一般取视。 火紅的嫁衣襯著肌膚如雪硝皂。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天作谭,我揣著相機與錄音稽物,去河邊找鬼。 笑死折欠,一個胖子當(dāng)著我的面吹牛贝或,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播锐秦,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼咪奖,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了酱床?” 一聲冷哼從身側(cè)響起羊赵,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎扇谣,沒想到半個月后昧捷,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡罐寨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年靡挥,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鸯绿。...
    茶點故事閱讀 40,424評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡跋破,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出楞慈,到底是詐尸還是另有隱情幔烛,我是刑警寧澤,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布囊蓝,位于F島的核電站,受9級特大地震影響令蛉,放射性物質(zhì)發(fā)生泄漏聚霜。R本人自食惡果不足惜狡恬,卻給世界環(huán)境...
    茶點故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蝎宇。 院中可真熱鬧弟劲,春花似錦、人聲如沸姥芥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽凉唐。三九已至庸追,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間台囱,已是汗流浹背淡溯。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留簿训,地道東北人咱娶。 一個月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像强品,于是被迫代替她去往敵國和親膘侮。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,435評論 2 359

推薦閱讀更多精彩內(nèi)容