Ios_sn_coredata (b)

Source: Internet
Author: User

The previous article is about how to carry out the basic operation of CoreData, this article is about Nsfetchedresultscontroller get CoreData data how with tableview perfect combination, and animation operation implementation.

The combination of the results of Nsfetchedresultscontroller and TableView

-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {    return [[self.querydata sections] count];} -(Nsinteger) TableView: (UITableView *) Table numberofrowsinsection: (nsinteger) Section {    if ([[Self.querydata Sections] Count] > 0) {        id <NSFetchedResultsSectionInfo> sectioninfo = [[self.querydata sections] Objectatindex:section];        return [Sectioninfo numberofobjects];    } else        return 0;}

  

Implementation of Nsfetchedresultscontrollerdelegate Proxy method

The method by which the data in the CoreData will change the call

-(void) Controllerwillchangecontent: (Nsfetchedresultscontroller *) Controller {        //The fetch controller is about to Start sending change notifications, so prepare the table view for updates.    [Self.tableview beginupdates];}

CoreData the method of changing calls in the row data can implement some animations when the data changes

-(void) Controller: (Nsfetchedresultscontroller *) controller didchangeobject: (ID) anobject Atindexpath: (Nsindexpath * ) Indexpath Forchangetype: (nsfetchedresultschangetype) type Newindexpath: (Nsindexpath *) Newindexpath {UITableView *t        Ableview = Self.tableview; Switch (type) {case Nsfetchedresultschangeinsert: [TableView insertrowsatindexpaths:@[newind            Expath] withrowanimation:uitableviewrowanimationright];                    Break Case Nsfetchedresultschangedelete: [TableView Deleterowsatindexpaths:@[indexpath] Withrowanimation:uitableviewr            Owanimationbottom];                    Break Case nsfetchedresultschangeupdate: [TableView Reloadrowsatindexpaths:@[indexpath] Withrowanimation:uitableviewr            Owanimationleft];                    Break Case Nsfetchedresultschangemove: [TableView Deleterowsatindexpaths:@[indexpath] Withrowanimation:uitableviewrow            Animationautomatic]; [tabLeview Insertrowsatindexpaths:@[newindexpath] withrowanimation:uitableviewrowanimationautomatic];    Break }}

The method that is called when the data in the CoreData group is changed can also implement some animations

-(void) Controller: (Nsfetchedresultscontroller *) controller didchangesection: (ID <nsfetchedresultssectioninfo >) sectioninfo atindex: (nsuinteger) Sectionindex Forchangetype: (nsfetchedresultschangetype) type{    switch ( Type) {case                    Nsfetchedresultschangeinsert:            [Self.tableview insertsections:[nsindexset Indexsetwithindex: Sectionindex] withrowanimation:uitableviewrowanimationautomatic];            break;        Case Nsfetchedresultschangemove: Break            ;        Case nsfetchedresultschangeupdate: Break            ;        Case Nsfetchedresultschangedelete:            [Self.tableview deletesections:[nsindexset Indexsetwithindex:sectionindex] Withrowanimation:uitableviewrowanimationautomatic];            break;    }}

Method called when data in CoreData has changed

-(void) Controllerdidchangecontent: (Nsfetchedresultscontroller *) Controller {    self.datalist = self.queryData.fetchedObjects.mutableCopy;    NSLog (@ "%@", self.datalist);    [Self.tableview endupdates];}

  

Ios_sn_coredata using Demo

Ios_sn_coredata (b)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.