zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Tarjan算法求双连通分量
    Tarjan
    前端技术实现js图片水印
    记录一下ionic canvas图片,还有canvas里面的图片跨域的问题
    ionic cordova screenshot 使用和操作
    关于ionic2 更新到ionic3 后组件不能用的解决方案
    背景图处理,这是个好东西记录一下
    radio样式的写法,单选和多选如何快速的改变默认样式,纯CSS,
    ionic使用cordova插件中的Screenshot截图分享功能
    ionic中执行pop返回上一个页面,还需要执行操作
  • 原文地址:https://www.cnblogs.com/jiurong001/p/5201116.html
Copyright © 2011-2022 走看看