美文网首页
Xcode8后CoreData的使用

Xcode8后CoreData的使用

作者: 有缘人2830 | 来源:发表于2017-05-11 17:18 被阅读26次
E9322D5A-7317-4297-B55F-007B4D452E61.png

一、创建xxx.xcdatamodeld的可视化模型文件 eg:MyCoreData.xcdatamodeld
并且添加两个属性 name age

直接上代码:

import "ViewController.h"

import "AppDelegate.h"

import "FirstCoreData+CoreDataClass.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
/****** table ******/
@property (strong, nonatomic) UITableView *myTable;
/****** dataSourse ******/
@property (strong, nonatomic) NSMutableArray *dataSourse;

/****** appdelegate ******/
@property (strong, nonatomic) AppDelegate *myAppdelegate;
@end

@implementation ViewController

pragma mark - lazy

  • (UITableView *)myTable{
    if (!_myTable) {
    _myTable = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _myTable.delegate = self;
    _myTable.dataSource = self;
    _myTable.rowHeight = 50;
    }
    return _myTable;
    }
  • (NSMutableArray *)dataSourse{
    if (!_dataSourse) {
    _dataSourse = [NSMutableArray array];
    }
    return _dataSourse;
    }

pragma mark - 系统

  • (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //查询数据
    //创建NSFetchRequest对象
    NSFetchRequest *fetchRequest= [[NSFetchRequest alloc]initWithEntityName:@"FirstCoreData"];
    //排序
    NSSortDescriptor *sort = [[NSSortDescriptor alloc]initWithKey:@"age" ascending:YES];
    fetchRequest.sortDescriptors = @[sort]; //这可以添加多种排序
    NSError *error = nil;
    NSArray *tempArray = [self.myAppdelegate.persistentContainer.viewContext executeFetchRequest:fetchRequest error:&error];

    if (error) {
    NSLog(@"查询失败:%@",error);
    }else{

      [self.dataSourse addObjectsFromArray:tempArray];  //添加到数据
    

    }

}

  • (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"coreDataTest";
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addData)];
    self.navigationItem.rightBarButtonItem = right;

    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"长按修改" style:UIBarButtonItemStylePlain target:self action:@selector(change)];
    self.navigationItem.leftBarButtonItem = left;

    [self.view addSubview:self.myTable];
    self.myAppdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

}

pragma mark - addData

  • (void)change{

}

  • (void)addData{
    //添加数据
    NSEntityDescription *description = [NSEntityDescription entityForName:@"FirstCoreData" inManagedObjectContext:self.myAppdelegate.persistentContainer.viewContext];
    FirstCoreData *first = [[FirstCoreData alloc]initWithEntity:description insertIntoManagedObjectContext:self.myAppdelegate.persistentContainer.viewContext];
    first.name = @"ZhangSan";
    int age = arc4random() % 30 + 1;
    first.age = [NSString stringWithFormat:@"%d",age];
    [self.dataSourse addObject:first]; //添加数据源
    [self.myTable insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.dataSourse.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft]; //table插入数据
    [self.myAppdelegate saveContext]; //保存数据

}

pragma mark - table delegate datasourse

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
    }

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourse.count;
    }

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellID = [NSString stringWithFormat:@"%ld%ld",(long)indexPath.section,(long)indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    //获取数据
    FirstCoreData *first = self.dataSourse[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"name:%@ age:%@",first.name, first.age];
    // 添加长按手势操作
    UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(cellLongPress:)];
    [cell addGestureRecognizer:longPressGesture];

    return cell;
    }
    //删除

  • (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
    //1.获取数据源
    FirstCoreData *first = self.dataSourse[indexPath.row];
    //2.删除数据数据源
    [self.dataSourse removeObject:first];
    //3.删除单元格
    [self.myTable deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    //4.删除数据管理器中的数据
    [self.myAppdelegate.persistentContainer.viewContext deleteObject:first];
    //5.保存
    [self.myAppdelegate saveContext];

    }
    }

  • (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
    }
    // 长按修改

  • (void)cellLongPress:(UILongPressGestureRecognizer *)tap{

    CGPoint index = [tap locationInView:self.myTable];
    NSIndexPath *indexPath = [self.myTable indexPathForRowAtPoint:index]; //获取行数
    UITableViewCell *cell = (UITableViewCell *)tap.view; //强转cell

    FirstCoreData *first = self.dataSourse[indexPath.row];
    first.name = @"修改后的闪闪发光";
    cell.textLabel.textColor = [UIColor orangeColor];
    [self.myTable reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    //保存数据
    [self.myAppdelegate saveContext];

}

x下载地址:http://code.cocoachina.com/view/135025

相关文章

  • Linker Error CoreData

    在 Xcode8 后使用 CoreData 创建 Model 后,编译时会产生 linker error,原因是 ...

  • Xcode8 使用 CoreData 创建 NSManageOb

    码上行动 升级 Xcode8 以上后,在使用 coreData 的时候发现新建的文件里找不到 NSManageOb...

  • Xcode8后CoreData的使用

    一、创建xxx.xcdatamodeld的可视化模型文件 eg:MyCoreData.xcdatamodeld并且...

  • Xcode8使用CoreData创建实体类的问题

    使用Xcode8之后使用CoreData创建实体对象的类和扩展类后,运行工程发现工程报错 这个问题是在XCode8...

  • Xcode8后coreData的使用简介

    该文章本来是以前在CSDN上写的,后面由于编辑方式不如简书,就将其copy到了简书。下面就简单的介绍一下怎么使用c...

  • Xcode8使用CoreData

    1.新建项目,勾选Use Core Data 2.选中xxx.Xcdatamodeld文件,点击序号2处的按钮,会...

  • xcode8使用coredata

    昨晚突然有个朋友问起coredata的使用,后来便自己写了个demo看看,发现跟Xcode7使用有些不一样,今天有...

  • Xcode8使用CoreData

    1.新建项目,勾选Use Core Data 2.选中xxx.Xcdatamodeld文件,点击序号2处的按钮,会...

  • Xcode8 CoreData的使用

    CoreData并不是一种新兴的技术,但是对于一般刚入门的初级开发者还是有一层神秘面纱存在的。下面简单介绍...

  • iOS CoreData版本升级和数据库迁移

    那如何实现数据库迁移呢?大概需要这几个步骤: PS: Xcode8 系统CoreData类做了不少改动,当然使用起...

网友评论

      本文标题:Xcode8后CoreData的使用

      本文链接:https://www.haomeiwen.com/subject/qqzotxtx.html