CoreData的一些簡單運用

1.首先建立一個新的工程數據庫

記得勾選下面的 Use Core Data數組

萬惡分割線————————————————————————app

 

而後點擊Add Entity 建立一個相似於表名。框架

萬惡分割線————————————————————————dom

而後建立屬性名 ,  選擇屬性的類型atom

萬惡分割線————————————————————————orm

而後你寫好屬性以後,就能夠讓系統幫你生成類了,點擊倒數第三個(Create NSManagedObject Subclass...)對象

萬惡分割線————————————————————————blog

記得,你要用就要勾選它,而後點擊下一步。排序

萬惡分割線————————————————————————

而後系統就幫你生成了類。

而後這個時候你能夠進到AppDElegate.h

 

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
//被管理對象上下文(數據管理器)至關於一個臨時數據庫
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
//被管理對象模型(數據模型器)
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
//持久化存儲助理(數據鏈接器)整個CoreData框架中的核心
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
//把罵我呢臨時數據庫中進行的改變進行永久保存
- (void)saveContext;
//獲取真實文件的存儲路徑,NURL類型
- (NSURL *)applicationDocumentsDirectory;

 

在你ViewController.m文件裏面就能夠實現數據庫的增刪改, 注意!!!!! 個人tableview,是拖拽控件的,可是裏面的方法幾乎均可以借鑑。

#import "AppDelegate.h"
#import "Clothes.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *TableVire;
@property(strong,nonatomic)NSMutableArray *dataSource;

//聲明一個AppDelegate對象屬性,用來調用類中的屬性,好比被管理對象上下文
@property(strong,nonatomic)AppDelegate *myAppDelegate;

@end

@implementation ViewController
/**
 *  插入數據
 *
 *  @param sender +barButtonItem
 */
- (IBAction)addModel:(id)sender
{
    //插入數據
    //建立實體描述對象
    NSEntityDescription *description=[NSEntityDescription entityForName:@"Clothes" inManagedObjectContext:self.myAppDelegate.managedObjectContext];
    
    //1.建立一個模型對象
    Clothes *cloth=[[Clothes alloc] initWithEntity:description insertIntoManagedObjectContext:self.myAppDelegate.managedObjectContext];
    cloth.name=@"Puma";
    int price=arc4random()%1000+1;
    cloth.price=[NSNumber numberWithInt:price];
    //插入數據源數組
    [self.dataSource addObject:cloth];
    //插入UI
    [self.TableVire insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.dataSource.count-1 inSection: 0]] withRowAnimation:UITableViewRowAnimationLeft];
    //對數據管理器中的更改進行永久存儲
    [self.myAppDelegate saveContext];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化數組
    self.dataSource=[NSMutableArray array];
    self.myAppDelegate=[UIApplication sharedApplication].delegate;
    //查詢數據
    //1.NSFetchRequest對象
    NSFetchRequest *request=[[NSFetchRequest alloc] initWithEntityName:@"Clothes"];
    //2.設置排序
    //2.1設置排序描述對象
    NSSortDescriptor *sortDescriptor=[[NSSortDescriptor alloc] initWithKey:@"price" ascending:YES];
    request.sortDescriptors=@[sortDescriptor];
    //執行這個查詢請求
    NSError *error=nil;
   NSArray *resut= [self.myAppDelegate.managedObjectContext executeFetchRequest:request error:&error];
    //給數據源組中添加數據
    [self.dataSource addObjectsFromArray:resut];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    Clothes *cloth=self.dataSource[indexPath.row];
    cell.textLabel.text=[NSString stringWithFormat:@"%@---%@",cloth.name,cloth.price];
    
    return cell;
}
//容許tableview編輯
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//tableView編輯的方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle==UITableViewCellEditingStyleDelete) {
        // 刪除數據源
        Clothes *cloth=self.dataSource[indexPath.row];
        [self.dataSource removeObject:cloth];
        //刪除數據管理器中的數據
        [self.myAppDelegate.managedObjectContext deleteObject:cloth];
        //將進行的更改進行永久保存
        [self.myAppDelegate saveContext];
        //刪除單元格
        [self.TableVire deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
    }
}
//點擊cell進行修改數據
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.找到模型對象
    Clothes *cloth=self.dataSource[indexPath.row];
    cloth.name=@"Nike";
//  舒心單元格
    [self.TableVire reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    //經過saveContext對數據驚醒永久保存
    [self.myAppDelegate saveContext];
  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

這樣你就對tableview進行增刪改,查看, 而且數據會永久存儲在你的本地數據庫 , 固然這個是很基本,很簡單的。

相關文章
相關標籤/搜索