CoreData

  viewController .m 内部的写法:

1 首先在新建的时候需要勾选  Use Core Data 选项

2 需要创建 coreData 数据

#import "ViewController.h"

#import "AppDelegate.h"

#import "Student+CoreDataProperties.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>//接受tableView的代理

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

//声明对像属性,调用类中的属性,如:被管理对象

@property (nonatomic, strong) AppDelegate *myAppDelegate;

@end

@implementation ViewController

//导航控制器的 添加按钮

- (IBAction)addModle:(UIBarButtonItem *)sender {

    

    //插入数据

    NSEntityDescription *description = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.myAppDelegate.managedObjectContext];

    //首先创建模型

    Student *student = [[Student alloc] initWithEntity:description insertIntoManagedObjectContext:self.myAppDelegate.managedObjectContext];

    NSArray *array1 = @[@"李小龙",@"梅艳芳",@"乔布斯",@"林志玲",@"乔丹"];

    NSArray *array2 = @[@"男",@"女"];    

    int age = arc4random()%(40 - 10 + 1) + 10;

    int x = arc4random() % 2;

    int y = arc4random() % 5;

    student.age = [NSNumber numberWithInt:age];

    student.name = [array1 objectAtIndex:y];

    student.gender = [array2 objectAtIndex:x];    

    //把数据模型添加到数组

    [self.dataArray addObject:student];

    //添加到  tableView 表视图

    [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.dataArray.count - 1  inSection:0]] withRowAnimation:UITableViewRowAnimationFade];//

    //对数据管理器中的更改进行永久存储

    [self.myAppDelegate saveContext];    

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataArray.count;

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    

   //在可视化变成内部已经 设置好 标签  “cell”

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    //找到模型

    Student *student = self.dataArray[indexPath.row];

    cell.textLabel.text =  [NSString stringWithFormat:@"%@-%@-%@",student.name,student.gender,student.age];

    return cell;

//允许编辑

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    return YES;

}

//编辑执行的方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        //删除数据源

        Student *stu = self.dataArray[indexPath.row];

        [self.dataArray removeObject:stu];

        //删除数据管理器的数据

        [self.myAppDelegate.managedObjectContext deleteObject:stu];

        //将进行更改的数据  进行永久保存

        [self.myAppDelegate saveContext];

        //删除段元格

        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }

}

//点击cell的方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"section:%ld,row:%ld",indexPath.section, indexPath.row);

- (void)viewDidLoad {

    [super viewDidLoad];    

  //设置导航控制器的标题

    self.navigationItem.title = @"超级明星";    

    //初始化存放数据的数组

    self.dataArray = [NSMutableArray arrayWithCapacity:0];

    //初始化代理

    self.myAppDelegate = [UIApplication sharedApplication].delegate;    

    //查询数据

    // 1 NSFetchRequst对象

    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];

    //2 设置排序

    //2.1 创建排序描述对象

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];//是否升序 给yes

    //返回值是数组

    request.sortDescriptors = @[sortDescriptor];    

    //执行查询请求

    NSError *error = nil;

    NSArray *result = [self.myAppDelegate.managedObjectContext executeFetchRequest:request error:&error];

    //给数据源数组添加数据

    [self.dataArray addObjectsFromArray:result];

    

    // Do any additional setup after loading the view, typically from a nib.

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

@end

原文地址:https://www.cnblogs.com/jiurong001/p/5201116.html