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
// 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
相关文章推荐
- [Unity] 使用Profiler.BeginSample()定位性能热点
- UVA 1160——X-Plosives
- Spring mvc 数据绑定及返回数据
- iOS 栈和堆的区别
- ListView + EditText
- 多态
- iOS 自定义UITableViewCell上添加UIButton按钮如何确定点击了哪一行,代码块实现
- 2016.3.16高项作业
- mysql 将查询结果拼接输出到本地文件
- 选IT不再让我迷茫
- GoldenGate配置文档,参数说明,常见错误处理
- 烂泥:Server-U FTP与AD完美集成方案详解
- java 小波变换
- 圣思源-张龙-Java SE 第十讲(面向对象之封装) 续
- SVN安装与其服务器搭建
- 单元测试
- 安全算法
- 如何通过 OAuth 2.0 使 iOS Apps 集成 LinkedIn 登录功能?
- 升级openssl
- pc手机&姓名&地址验证&input提示文案兼容ie