CoreData的使用

//

//  Province.h

//  MyCoreData

//

//  Created by zmx on 16/3/21.

//  Copyright © 2016 zmx. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface Province : NSObject


@property (nonatomic, copy) NSString *name;

@property (nonatomic, copy) NSString *capital;


@end



//

//  ViewController.m

//  MyCoreData

//

//  Created by zmx on 16/3/21.

//  Copyright © 2016 zmx. All rights reserved.

//


#import "ViewController.h"

#import "EditViewController.h"

#import "AppDelegate.h"

#import "Province.h"


@interface ViewController () <UITableViewDataSource, UITableViewDelegate>


@property (weak, nonatomic) IBOutlet UITableView *tableView;


@property (nonatomic, strong) NSMutableArray *provinces;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}


- (void)viewWillAppear:(BOOL)animated {

    [self query];

}


- (void)query {

    NSManagedObjectContext *context = ((AppDelegate *)[UIApplication sharedApplication].delegate).managedObjectContext;

    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Province"];

    self.provinces = [NSMutableArray arrayWithArray:[context executeFetchRequest:request error:nil]];

    [self.tableView reloadData];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.provinces.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *identifier = @"province";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

    }

    

    Province *province = self.provinces[indexPath.row];

    cell.textLabel.text = [NSString stringWithFormat:@"%@  %@", province.name, province.capital];

    return cell;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    EditViewController *editViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"editViewController"];

    editViewController.province = self.provinces[indexPath.row];

    //先设置province属性,再设置view背景颜色,否则province属性未设置,viewDidLoad方法已调用

    editViewController.view.backgroundColor = [UIColor whiteColor];

    [self.navigationController pushViewController:editViewController animated:YES];

}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

    return YES;

}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        NSManagedObjectContext *context = ((AppDelegate *)[UIApplication sharedApplication].delegate).managedObjectContext;

        [context deleteObject:self.provinces[indexPath.row]];

        [context save:nil];

        [self.provinces removeObjectAtIndex:indexPath.row];

        [self.tableView reloadData];

    }

}


@end



//

//  EditViewController.h

//  MyCoreData

//

//  Created by zmx on 16/3/21.

//  Copyright © 2016 zmx. All rights reserved.

//


#import <UIKit/UIKit.h>


@class Province;


@interface EditViewController : UIViewController


@property (nonatomic, strong) Province *province;


@end



//

//  EditViewController.m

//  MyCoreData

//

//  Created by zmx on 16/3/21.

//  Copyright © 2016 zmx. All rights reserved.

//


#import "EditViewController.h"

#import "AppDelegate.h"

#import "Province.h"


@interface EditViewController ()


@property (weak, nonatomic) IBOutlet UITextField *nameLabel;

@property (weak, nonatomic) IBOutlet UITextField *capitalLabel;


@end


@implementation EditViewController


- (IBAction)send:(id)sender {

    NSManagedObjectContext *context = ((AppDelegate *)[UIApplication sharedApplication].delegate).managedObjectContext;

    if (!self.province) {

        self.province = (Province *)[NSEntityDescription insertNewObjectForEntityForName:@"Province" inManagedObjectContext:context];

    }

    self.province.name = self.nameLabel.text;

    self.province.capital = self.capitalLabel.text;

    [context save:nil];

    [self.navigationController popViewControllerAnimated:YES];

}


- (void)viewDidLoad {

    [super viewDidLoad];

    

    if (self.province) {

        self.nameLabel.text = self.province.name;

        self.capitalLabel.text = self.province.capital;

    }

}

@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值