OC 數(shù)據(jù)庫CoreData

DataBase.h

import <Foundation/Foundation.h>

import "Entity+CoreDataClass.h"

import "AppDelegate.h"

@interface DataBase : NSObject

+(instancetype)showdata;

-(void)addname:(NSDictionary *)dic;

-(void)changedata;

-(void)deletdata:(Entity *)theData;

-(NSMutableArray*)showAllArray;
DataBase.m

import "DataBase.h"

static DataBase *thedatabase;
@implementation DataBase

+(instancetype)showdata{

if (!thedatabase) {
    thedatabase = [[DataBase alloc]init];
}
return thedatabase;

}

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

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

Entity *person = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:app.persistentContainer.viewContext];
person.personName = dic[@"personName"];
person.personAge = dic[@"personAge"];

[app saveContext];

}

-(void)changedata{

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

[app saveContext];

}

-(void)deletdata:(Entity *)theData{

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

[app.persistentContainer.viewContext deleteObject:theData];

[app saveContext];

}

-(NSMutableArray*)showAllArray{

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

NSFetchRequest *request = [[NSFetchRequest alloc]init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entity" inManagedObjectContext:app.persistentContainer.viewContext];

[request setEntity:entity];

NSArray *arr = [app.persistentContainer.viewContext executeFetchRequest:request error:nil];

return [arr mutableCopy];

}

@end
視圖.h

import <UIKit/UIKit.h>

@interface AddView : UIView

@property(nonatomic,strong)UITextField *theBookTF;
@property(nonatomic,strong)UITextField *thePriceTF;

@end

視圖.m

import "AddView.h"

@implementation AddView

-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {

    [self addSubview:self.theBookTF];
    [self addSubview:self.thePriceTF];
}
return self;

}

pragma mark - 屬性實(shí)例化

-(UITextField *)theBookTF
{
if (!_theBookTF) {
_theBookTF = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
_theBookTF.center = CGPointMake(self.center.x, self.center.y - 50);
_theBookTF.layer.borderWidth = 1.0;
_theBookTF.borderStyle = UITextBorderStyleRoundedRect;
_theBookTF.placeholder = @"姓名";
}
return _theBookTF;
}

-(UITextField *)thePriceTF
{
if (!_thePriceTF) {
_thePriceTF = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
_thePriceTF.center = CGPointMake(self.center.x, self.center.y + 10);
_thePriceTF.borderStyle = UITextBorderStyleRoundedRect;
_thePriceTF.layer.borderWidth = 1.0;
_thePriceTF.placeholder = @"年齡";
}
return _thePriceTF;
}

@end
主控制器.h

import <UIKit/UIKit.h>

@interface ViewController : UITableViewController

@end
主控制器.m

import "ViewController.h"

import "DataBase.h"

import "Entity+CoreDataClass.h"

import "AddViewController.h"

@interface ViewController ()
//接收數(shù)組
@property(nonatomic,strong)NSArray *theDataArr;

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"圖書管理信息平臺";
    //導(dǎo)航條按鈕
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(addName)];
    }

-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//
self.theDataArr = [[DataBase showdata
]showAllArray];
//刷新表格
[self.tableView reloadData];
}

-(void)addName
{
// 進(jìn)入Add視圖
AddViewController *addVC = [[AddViewController alloc] init];

[self.navigationController pushViewController:addVC animated:YES];

}

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.theDataArr.count;
    }

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

Entity *enti = self.theDataArr [indexPath.row];

[[DataBase showdata]deletdata:enti];

self.theDataArr = [[DataBase showdata]showAllArray];

[self.tableView reloadData];

}

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *identifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    Entity *theEntity = self.theDataArr[indexPath.row];

    cell.textLabel.text = theEntity.personName;
    cell.detailTextLabel.text = theEntity.personAge;

    return cell;
    }

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

AddViewController *theVc = [[AddViewController alloc]init];

theVc.theEntity = self.theDataArr[indexPath.row];

[self.navigationController pushViewController:theVc animated:YES];

}

@end
添加控制器.h

import <UIKit/UIKit.h>

import "Entity+CoreDataClass.h"

@interface AddViewController : UIViewController

@property(nonatomic,strong)Entity *theEntity;

@end
添加控制器.m

import "AddViewController.h"

import "AddView.h"

import "Entity+CoreDataClass.h"

import "DataBase.h"

@interface AddViewController ()
{
AddView *theAddView;

}

@end

@implementation AddViewController

  • (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"添加數(shù)據(jù)平臺";

theAddView = [[AddView alloc] initWithFrame:self.view.frame];
theAddView.backgroundColor = [UIColor whiteColor];
self.view = theAddView;

theAddView.theBookTF.text =  self.theEntity.personName;

theAddView.thePriceTF.text = self.theEntity.personAge;

if (theAddView.theBookTF.text == nil ||theAddView.theBookTF.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(update)];

}
//添加
-(void)save
{

NSDictionary *dict = @{@"personName":theAddView.theBookTF.text,@"personAge":theAddView.thePriceTF.text};

// CoreDataModel *data = [[CoreDataModel alloc]init];
// data.dataage = theAddView.theBookTF.text;
// data.dataname = theAddView.thePriceTF.text;

[[DataBase showdata] addname:dict];

[self.navigationController popViewControllerAnimated:YES];

}
//修改
-(void)update{

self.theEntity.personName=theAddView.theBookTF.text;

self.theEntity.personAge = theAddView.thePriceTF.text;

[[DataBase showdata]changedata];

[self.navigationController popViewControllerAnimated:YES];

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末寇钉,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子悠栓,更是在濱河造成了極大的恐慌贴汪,老刑警劉巖剑梳,帶你破解...
    沈念sama閱讀 218,640評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件乃沙,死亡現(xiàn)場離奇詭異熬甫,居然都是意外死亡猎塞,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評論 3 395
  • 文/潘曉璐 我一進(jìn)店門杠纵,熙熙樓的掌柜王于貴愁眉苦臉地迎上來荠耽,“玉大人,你說我怎么就攤上這事比藻÷亮浚” “怎么了?”我有些...
    開封第一講書人閱讀 165,011評論 0 355
  • 文/不壞的土叔 我叫張陵银亲,是天一觀的道長慢叨。 經(jīng)常有香客問我,道長务蝠,這世上最難降的妖魔是什么拍谐? 我笑而不...
    開封第一講書人閱讀 58,755評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上轩拨,老公的妹妹穿的比我還像新娘践瓷。我一直安慰自己,他們只是感情好亡蓉,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,774評論 6 392
  • 文/花漫 我一把揭開白布晕翠。 她就那樣靜靜地躺著,像睡著了一般砍濒。 火紅的嫁衣襯著肌膚如雪淋肾。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,610評論 1 305
  • 那天爸邢,我揣著相機(jī)與錄音樊卓,去河邊找鬼。 笑死甲棍,一個(gè)胖子當(dāng)著我的面吹牛简识,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播感猛,決...
    沈念sama閱讀 40,352評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼七扰,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了陪白?” 一聲冷哼從身側(cè)響起颈走,我...
    開封第一講書人閱讀 39,257評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎咱士,沒想到半個(gè)月后立由,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,717評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡序厉,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,894評論 3 336
  • 正文 我和宋清朗相戀三年锐膜,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片弛房。...
    茶點(diǎn)故事閱讀 40,021評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡道盏,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出文捶,到底是詐尸還是另有隱情荷逞,我是刑警寧澤,帶...
    沈念sama閱讀 35,735評論 5 346
  • 正文 年R本政府宣布粹排,位于F島的核電站种远,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏顽耳。R本人自食惡果不足惜坠敷,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,354評論 3 330
  • 文/蒙蒙 一妙同、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧常拓,春花似錦渐溶、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,936評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至掂恕,卻和暖如春拖陆,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背懊亡。 一陣腳步聲響...
    開封第一講書人閱讀 33,054評論 1 270
  • 我被黑心中介騙來泰國打工依啰, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人店枣。 一個(gè)月前我還...
    沈念sama閱讀 48,224評論 3 371
  • 正文 我出身青樓速警,卻偏偏與公主長得像,于是被迫代替她去往敵國和親鸯两。 傳聞我的和親對象是個(gè)殘疾皇子闷旧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,974評論 2 355

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