//
//
// AppDelegate.m
// CoreData
//
// Created by 王聪on 14/8/19.
// Copyright (c) 2014年 Congwang. All rights reserved.
//
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOp tions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//模型版本升级步骤:
//1.新建模型版本 (选中可视化建模文件, 点击editor, 选择addModelVersion)
//2.把当前新建的模型版本设置成选中状态 (先选中总的模型版本, 然后在属性检查器中的第一个标签有个current,选中新建的即可)
//3.创建映射文件(cmd + n , 在coreData选项卡中, 选择mappingModel), 记住source是老的模型版本, target是新的模型版本
//4.在数据连接器中的懒加载方法中, option选项由nil变成一个字典, key为NSMigrate, value为@{YES}, 该选项的意思是, 模型版本自动升级 (数据库升级)
//
//
//
//
//
//
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOp
}
- (void)applicationWillResignAct
}
- (void)applicationDidEnterBackg
}
- (void)applicationWillEnterFore
}
- (void)applicationDidBecomeActi
}
- (void)applicationWillTerminate
}
#pragma mark - Core Data stack
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinat
- (NSURL *)applicationDocumentsDire
}
//
- (NSManagedObjectModel *)managedObjectModel {
}
- (NSPersistentStoreCoordin
}
//数据管理器的懒加载 (重写getter方法)
- (NSManagedObjectContext *)managedObjectContext {
}
#pragma mark - Core Data Saving support
- (void)saveContext {
}
@end
// ViewController.m
// CoreData
//
// Created by 王聪 on 14/8/19.
// Copyright (c) 2014
年
Congwang. All rights reserved.
//
#import "ViewController.h"
#import "Clothes.h"
#import "AppDelegate.h"
@interface ViewController ()<</span>UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) AppDelegate *myAppDelegate;
@property (strong, nonatomic) NSMutableArray *dataSource;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.myAppDelegate = [UIApplicationsharedApplication].delegate;
//初始化数组
self.dataSource = [NSMutableArray array]; //进行数据查询 -- coreData查询
//1.创建数据查询对象
NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"Clothes"];
//2.创建排序描述对象(可选择, 想进行排序的时候在写)
NSSortDescriptor *sortDescription = [[NSSortDescriptoralloc] initWithKey:@"price" ascending:YES];
//2.1给request的排序属性赋值
request.sortDescriptors = @[sortDescription];
// //3.创建检索条件(可选择)
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@ and price > %@", @"Nike", [NSNumber numberWithFloat:1.0]];
// request.predicate = predicate;
NSError *error = nil;
NSArray *resultArray = [self.myAppDelegate.managedObjectContextexecuteFetchRequest:request error:&error];
//
[self.dataSource addObjectsFromArray:resultArray];
}
#pragma mark - tableView的代理和dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithI dentifier:@"cell"];
Clothes *cloth = self.dataSource[indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@ -- %@", cloth.name, cloth.price];
return cell;
}
//点击cell触发的代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//修改数据
Clothes *cloth = self.dataSource[indexPath.row];
cloth.name = @"Nike";
//将修改进行永久保存
[self.myAppDelegate saveContext];
//刷新单元格
[self.tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationA utomatic];
}
//允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingSt yle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingSt yleDelete) {
//1.先删除数据源
Clothes *clothe = self.dataSource[indexPath.row];
//在关于布局dataSource中, 删除数据
[self.dataSource removeObject:clothe];
//要在真实的文件中删除 (*coreData中的删除)
[self.myAppDelegate.managedObjectContextdeleteObject:clothe];
//对数据管理器进行改变, 保存到真实的文件当中
[self.myAppDelegate saveContext];
//2.再删除单元格
[self.tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationF ade];
}
}
//添加数据
- (IBAction)addModel:(id)sender {
// 1.先插数据源
//coreData中持久化操作
//快速插入方法 (推荐用)
Clothes *clothNew = [NSEntityDescriptioninsertNewObjectForEntity ForName:@"Clothes"inManagedObjectContext:self.myAppDelegate.managedObjectContext];
//给对象的属性赋值
//实体描述
NSEntityDescription *description = [NSEntityDescriptionentityForName:@"Clothes"inManagedObjectContext:self.myAppDelegate.managedObjectContext];
//创建模型对象
Clothes *clothe = [[Clothes alloc]initWithEntity:description insertIntoManagedObjectC ontext:self.myAppDelegate.managedObjectContext];
clothe.name = @"Prada";
float price = (arc4random() % 1000 +1) / 1.1;
clothe.price = [NSNumber numberWithFloat:price];
//添加到数据源中 插入数据源
[self.dataSource addObject:clothe];
// 2.插入单元格
NSIndexPath *indexPath = [NSIndexPathindexPathForRow:self.dataSource.count - 1 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationL eft];
//对数据管理器中的数据进行永久保存 (保存在沙盒路径下的真是的文件中)
[self.myAppDelegate saveContext];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//
//
//
//
//
#import "ViewController.h"
#import "Clothes.h"
#import "AppDelegate.h"
@interface ViewController ()<</span>UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) AppDelegate *myAppDelegate;
@property (strong, nonatomic) NSMutableArray *dataSource;
@end
@implementation ViewController
- (void)viewDidLoad {
//
//
//
}
#pragma mark - tableView的代理和dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
}
//点击cell触发的代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
//允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingSt
}
//添加数据
- (IBAction)addModel:(id)sender {
}
- (void)didReceiveMemoryWarning {
}
@end