Plist

屏幕快照 2017-09-21 下午8.56.02.png

Plist文件書寫

屏幕快照 2017-09-21 下午8.57.40.png

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>
//接收數(shù)組
@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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末晓锻,一起剝皮案震驚了整個濱河市揍鸟,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌捌刮,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,627評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件颤诀,死亡現(xiàn)場離奇詭異名秀,居然都是意外死亡,警方通過查閱死者的電腦和手機侍郭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來掠河,“玉大人亮元,你說我怎么就攤上這事∵肽。” “怎么了爆捞?”我有些...
    開封第一講書人閱讀 169,346評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長勾拉。 經(jīng)常有香客問我煮甥,道長,這世上最難降的妖魔是什么望艺? 我笑而不...
    開封第一講書人閱讀 60,097評論 1 300
  • 正文 為了忘掉前任苛秕,我火速辦了婚禮肌访,結(jié)果婚禮上找默,老公的妹妹穿的比我還像新娘。我一直安慰自己吼驶,他們只是感情好惩激,可當(dāng)我...
    茶點故事閱讀 69,100評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蟹演,像睡著了一般风钻。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上酒请,一...
    開封第一講書人閱讀 52,696評論 1 312
  • 那天骡技,我揣著相機與錄音,去河邊找鬼。 笑死布朦,一個胖子當(dāng)著我的面吹牛囤萤,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播是趴,決...
    沈念sama閱讀 41,165評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼涛舍,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了唆途?” 一聲冷哼從身側(cè)響起富雅,我...
    開封第一講書人閱讀 40,108評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎肛搬,沒想到半個月后没佑,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,646評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡滚婉,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,709評論 3 342
  • 正文 我和宋清朗相戀三年图筹,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片让腹。...
    茶點故事閱讀 40,861評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡远剩,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出骇窍,到底是詐尸還是另有隱情瓜晤,我是刑警寧澤,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布腹纳,位于F島的核電站痢掠,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏嘲恍。R本人自食惡果不足惜足画,卻給世界環(huán)境...
    茶點故事閱讀 42,196評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望佃牛。 院中可真熱鬧淹辞,春花似錦、人聲如沸俘侠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,698評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽爷速。三九已至央星,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間惫东,已是汗流浹背莉给。 一陣腳步聲響...
    開封第一講書人閱讀 33,804評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人颓遏。 一個月前我還...
    沈念sama閱讀 49,287評論 3 379
  • 正文 我出身青樓胁黑,卻偏偏與公主長得像,于是被迫代替她去往敵國和親州泊。 傳聞我的和親對象是個殘疾皇子丧蘸,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,860評論 2 361

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