Coder Social home page Coder Social logo

coredata_study's Introduction

CoreData_Study

  • 在创建工程的时候,一定要选CoreData!一定要选CoreData!一定要选CoreData!

  • 进行storyboard的页面设置,创建tableView控件,

  • 将方法拖拽到ViewController中,进行实现,其中,引入了

    @property (strong, nonatomic) IBOutlet UITableView *tableView;
    
    - (IBAction)addModel:(id)sender{}
    
  • 既然设置了tableView,就必须要导入 delegate 的方法,所以

    @interface ViewController ()<UITableViewDataSource, UITableViewDelegate, UIApplicationDelegate>
    
  • 引入delegate协议之后,就必须实现 delegate 和 dataSource 的方法

    //返回分区中的行数
    - (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,(int64_t)cloth.price];
        
        return cell;
    }
    
  • 设置好必须实现的 delegate 和 dataSource 的方法之后,继续实现设置可以编辑表格,和点击每一cell的实现方法

    //允许编辑tableView
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
        return YES;
    }
    
    - (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.persistentContainer.viewContext deleteObject:cloth];
            
            //将进行更改进行永久保存
            [self.myAppDelegate saveContext];
            
            //删除单元格
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
    }
    
    //点击cell修改数据
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        //1、找到模型对象
        Clothes *cloth = self.dataSource[indexPath.row];
        
        cloth.name = @"NIKE";
        
        //更改数据源,刷新单元行
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        
        //更改之后进行永久保存
        [self.myAppDelegate saveContext];
        
    }
    
  • 创建这些方法之后,设置xcdatamodel并导入,实现作用方法。

  • 在每次添加数据之后,想数据存储,就先要使用NSFetchRequest查询数据,描述对象NSSortDescriptor

  • 切记,更改数据之后,就应该调用-(void)saveContext;方法进行数据的永久保存

  • 在需要删除数据的时候,首先要删除数据源_dataSource,再删除元祖_tableView的数据,最后实现saveContext永久保存

coredata_study's People

Contributors

jujuxw avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.