Plist文件书写
AppDelegate.h
#import <CoreData/CoreData.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (readonly, strong) NSPersistentContainer *persistentContainer;
- (void)saveContext;
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ViewController *vc =[[ViewController alloc]init];
UINavigationController *navVc = [[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = navVc;
return YES;
}
DataBase.h
#import <Foundation/Foundation.h>
#import "Entity+CoreDataClass.h"
#import "AppDelegate.h"
@interface DataBase : NSObject
+(instancetype)showdata;
-(void)addname:(NSDictionary *)dic;
-(void)deletdata:(Entity *)theData;
-(NSMutableArray*)showAllArray;
@end
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.name = dic[@"Name"];
person.age = dic[@"Age"];
[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
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
ViewController.m
#import "ViewController.h"
#import "NextViewController.h"
#import "DataBase.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>
{
NSArray *arr;
}
@property (nonatomic,strong)UITableView *tableView;
@end
@implementation ViewController
- (UITableView *)tableView
{
if (!_tableView)
{
_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
}
return _tableView;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view addSubview:self.tableView];
NSString *path = [[NSBundle mainBundle]pathForResource:@"MyPlist" ofType:@"plist"];
arr = [[NSArray alloc]initWithContentsOfFile:path];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(rightClick)];
}
- (void)rightClick
{
NextViewController *nextVc = [[NextViewController alloc]init];
[self.navigationController pushViewController:nextVc animated:YES];
NSDictionary *dict = @{@"Name":arr[0],@"Age":arr[1]};
[[DataBase showdata] addname:dict];
}
#pragma mark -
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellid = @"CELL";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
if (!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
}
cell.textLabel.text = arr[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"是否收藏" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
[alertV show];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
NextViewController.h
#import <UIKit/UIKit.h>
@interface NextViewController : UIViewController
@end
NextViewController.m
#import "NextViewController.h"
#import "Entity+CoreDataClass.h"
#import "DataBase.h"
@interface NextViewController ()<UITableViewDelegate,UITableViewDataSource>
//接收数组
@property(nonatomic,strong)NSArray *theDataArr;
@property (nonatomic,strong)UITableView *tableView;
@end
@implementation NextViewController
- (UITableView *)tableView
{
if (!_tableView)
{
_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
}
return _tableView;
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//
self.theDataArr = [[DataBase showdata
]showAllArray];
//刷新表格
[self.tableView reloadData];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view addSubview:self.tableView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.theDataArr.count;
}
- (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 *entity = self.theDataArr[indexPath.row];
cell.textLabel.text = entity.name;
cell.detailTextLabel.text = entity.age;
return cell;
}
-(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];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end