coredata

#import "DataManager.h"

#import "AppDelegate.h"

@interfaceDataManager(){

? ? //聲明一個AppDelegate對象屬性,調(diào)用里面的被管理的對象上下文 保存方法


? ? AppDelegate * myDelegate;


}

@end

@implementation DataManager

//單例創(chuàng)建

+(DataManager *)shareManager{

? ? staticDataManager * manager;

? ? if(!manager) {

? ? ? ? manager = [[DataManager alloc]init];

? ? }

? ? returnmanager;

}

//插入

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

?? //CoreData數(shù)據(jù)的插入

? ? //新方法 **

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

? ? //創(chuàng)建實體描述對象

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

? ? //創(chuàng)建一個模型對

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

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

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

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

? ? //對數(shù)據(jù)管理器的更改進行永久保存

? ? [myDelegate saveContext];


}

//更改

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

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

? ? [myDelegate saveContext];



}

//查找

-(NSArray *)select{

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

? ? //查詢數(shù)據(jù)

? ? //1.創(chuàng)建查詢請求

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

? ? //獲取地址

? ? NSError * error =nil;

? ? //2.執(zhí)行這個查詢請求

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

? ? // 查詢數(shù)據(jù)

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

? ? //點擊單元格跳轉(zhuǎn)界面

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

? ? ? ? //添加數(shù)據(jù)

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

? ? }else{

? ? ? ? self.title=@"修改數(shù)據(jù)";

? ? ? ? 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=@"添加數(shù)據(jù)";

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


? ? }else{

? ? ? ? //修改

? ? ? ? self.title=@"修改數(shù)據(jù)";

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末硝岗,一起剝皮案震驚了整個濱河市楷掉,隨后出現(xiàn)的幾起案子勃黍,更是在濱河造成了極大的恐慌贬蛙,老刑警劉巖伐债,帶你破解...
    沈念sama閱讀 221,406評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異哩俭,居然都是意外死亡绞蹦,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,395評論 3 398
  • 文/潘曉璐 我一進店門彤恶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來钞钙,“玉大人,你說我怎么就攤上這事声离∶⒘叮” “怎么了?”我有些...
    開封第一講書人閱讀 167,815評論 0 360
  • 文/不壞的土叔 我叫張陵术徊,是天一觀的道長本刽。 經(jīng)常有香客問我,道長赠涮,這世上最難降的妖魔是什么子寓? 我笑而不...
    開封第一講書人閱讀 59,537評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮笋除,結(jié)果婚禮上斜友,老公的妹妹穿的比我還像新娘。我一直安慰自己垃它,他們只是感情好鲜屏,可當我...
    茶點故事閱讀 68,536評論 6 397
  • 文/花漫 我一把揭開白布烹看。 她就那樣靜靜地躺著,像睡著了一般洛史。 火紅的嫁衣襯著肌膚如雪惯殊。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,184評論 1 308
  • 那天也殖,我揣著相機與錄音土思,去河邊找鬼。 笑死忆嗜,一個胖子當著我的面吹牛己儒,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播霎褐,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼址愿,長吁一口氣:“原來是場噩夢啊……” “哼该镣!你這毒婦竟也來了冻璃?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,668評論 0 276
  • 序言:老撾萬榮一對情侶失蹤损合,失蹤者是張志新(化名)和其女友劉穎省艳,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體嫁审,經(jīng)...
    沈念sama閱讀 46,212評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡跋炕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,299評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了律适。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片辐烂。...
    茶點故事閱讀 40,438評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖捂贿,靈堂內(nèi)的尸體忽然破棺而出纠修,到底是詐尸還是另有隱情,我是刑警寧澤厂僧,帶...
    沈念sama閱讀 36,128評論 5 349
  • 正文 年R本政府宣布扣草,位于F島的核電站,受9級特大地震影響颜屠,放射性物質(zhì)發(fā)生泄漏辰妙。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,807評論 3 333
  • 文/蒙蒙 一甫窟、第九天 我趴在偏房一處隱蔽的房頂上張望密浑。 院中可真熱鬧,春花似錦粗井、人聲如沸尔破。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,279評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽呆瞻。三九已至台夺,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間痴脾,已是汗流浹背颤介。 一陣腳步聲響...
    開封第一講書人閱讀 33,395評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留赞赖,地道東北人滚朵。 一個月前我還...
    沈念sama閱讀 48,827評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像前域,于是被迫代替她去往敵國和親辕近。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,446評論 2 359

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