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 - 属性实例化
-(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 ()
//接收数组
@property(nonatomic,strong)NSArray *theDataArr;
@end
@implementation ViewController
-
(void)viewDidLoad {
[super viewDidLoad];self.title = @"图书管理信息平台";
//导航条按钮
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
{
// 进入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 = @"添加数据平台";
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
网友评论