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)