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{
self.title=@"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{
self.title=@"CoreData单表";
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(SaveAction)];
if (self.typeID ==1)
{
//添加
self.title =@"添加数据";
}else{
// 修改
self.title=@"修改数据";
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