您的位置:首页 > 其它

CoreData的使用

2016-03-21 16:02 295 查看
//

//  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
erro
10e2a
r: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: