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进行增删改,查看, 而且数据会永久存储在你的本地数据库 , 固然这个是很基本,很简单的。