1. 程式人生 > >蘋果公司封裝的資料持久化的框架

蘋果公司封裝的資料持久化的框架

viewcontroller.m

#import "ViewController.h"
#import "twovc.h"
#import "DataManager.h"
#import "Student+CoreDataClass.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tbv;
@property (nonatomic ,strong) NSMutableArray *dataSource;
@end

@implementation ViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //查詢資料
    self.dataSource =[NSMutableArray arrayWithArray:[[DataManager shareManager]select]];
    [self.tbv reloadData];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self setNav];
    [self.view addSubview:self.tbv];



}
-(UITableView *)tbv{
    
    if (!_tbv)
    {
        _tbv=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tbv.delegate=self;
        _tbv.dataSource=self;
        
    }
    return _tbv;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *str = @"str";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str];
    if (cell==nil)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    Student *s=_dataSource[indexPath.row];
    
    cell.textLabel.text = [NSString stringWithFormat:@"stuID =%d.name =%@,age = %d",s.stuid,s.name,s.age];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //點選單元格跳轉頁面
    
    twovc *two=[[twovc alloc]init];
    two.student=_dataSource[indexPath.row];
    [self.navigationController pushViewController:two animated:YES];
    
    
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    //刪除資料庫
    [[DataManager shareManager]deleteData:_dataSource[indexPath.row]];
    //刪除資料來源資料
    [_dataSource removeObject: _dataSource[indexPath.row]];
    [self.tbv reloadData];
    
    
    
    
    
    
}


-(void)setNav{
    
    
[email protected]
"CoreData單表"; self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction)]; } -(void)addAction{ twovc *two =[[twovc alloc]init]; two.typeID =1;//代表新增 [self.navigationController pushViewController:two animated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end

two.h.m.xib

#import "twovc.h"
#import "DataManager.h"
@interface twovc ()
@property (weak, nonatomic) IBOutlet UITextField *stuIDTF;
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *age;

@end

@implementation twovc

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNav];
}
-(void)setNav{
    
    
[email protected]
"CoreData單表"; self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(SaveAction)]; if (self.typeID ==1) { //新增 self.title [email protected]"新增資料"; }else{ // 修改 [email protected]"修改資料"; self.stuIDTF.text =[NSString stringWithFormat:@"%d",_student.stuid]; self.name.text =_student.name; self.age.text =[NSString stringWithFormat:@"%d",_student.age]; } } -(void)SaveAction{ if (self.typeID ==1) { //新增方法 [[DataManager shareManager]insert:@{ @"stuid":@([_stuIDTF.text integerValue]),@"name":_name.text,@"age":@([_age.text integerValue]) }]; }else{ // 修改方法 self.student.stuid = [_stuIDTF.text integerValue]; self.student.name=_name.text; self.student.age=[_age.text integerValue]; [[DataManager shareManager]update:_student]; } [self.navigationController popViewControllerAnimated:YES]; } @end

在這裡插入圖片描述