ios - Delete Core Data Entry from Table View Controller -
i trying delete core data entry table view controller, when swipe left, delete button comes , deletes entry. unfortunately, getting error: "property 'classes' not found on type "classtableviewcontroller *" on line 251 , 260 (code posted below) , not sure do. have nsmutableobject class called classes.m , classes.h, , have been imported, xcode doesn't seem recognize it.
here code:
classtableviewcontroller.m
#import "classtableviewcontroller.h" #import "classes.m" #import "classes.h" #import "classtableviewcell.h" #import "newclassviewcontroller.h" #import <coredata/coredata.h> @interface classtableviewcontroller () - (void)configurecell:(classtableviewcell *)cell atindexpath:(nsindexpath *)indexpath; @end @implementation classtableviewcontroller @synthesize fetchedresultscontroller = _fetchedresultscontroller; @synthesize managedobjectcontext = _managedobjectcontext; @synthesize managedobjectmodel = _managedobjectmodel; - (id)initwithstyle:(uitableviewstyle)style { self = [super initwithstyle:style]; if (self) { // custom initialization } return self; } #pragma mark - segue - (void) prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if ( [[segue identifier] isequaltostring:@"showclass"] ){ newclassviewcontroller *dvc = (newclassviewcontroller *)[segue destinationviewcontroller]; nsindexpath *indexpath = [self.tableview indexpathforselectedrow]; classes *classid = [[self fetchedresultscontroller] objectatindexpath:indexpath]; [dvc setclassid:classid]; } else if ([[segue identifier] isequaltostring:@"addclass"]) { newclassviewcontroller *dvc = (newclassviewcontroller *)[[segue destinationviewcontroller] topviewcontroller]; [dvc setclassid:[nsentitydescription insertnewobjectforentityforname:@"myclassesid" inmanagedobjectcontext:self.managedobjectcontext]]; } } #pragma mark - view lifecycle - (void)viewdidload { [super viewdidload]; // uncomment following line preserve selection between presentations. // self.clearsselectiononviewwillappear = no; // uncomment following line display edit button in navigation bar view controller. // self.navigationitem.rightbarbuttonitem = self.editbuttonitem; self.navigationitem.leftbarbuttonitem = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemdone target:self action:@selector(firstdone)]; if (_managedobjectcontext == nil) { _managedobjectcontext = [(classtableviewcontroller *)[[uiapplication sharedapplication] delegate] managedobjectcontext]; nslog(@"after managedobjectcontext: %@", _managedobjectcontext); } } - (void)viewdidunload { [super viewdidunload]; // release retained subviews of main view. // e.g. self.myoutlet = nil; } - (void) firstdone { [self.navigationcontroller dismissmodalviewcontrolleranimated:yes]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } #pragma mark - table view data source - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { // return number of sections. return [[self.fetchedresultscontroller sections] count]; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { id <nsfetchedresultssectioninfo> sectioninfo = [[self.fetchedresultscontroller sections] objectatindex:section]; return [sectioninfo numberofobjects]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"classescell"; classtableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[classtableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } [self configurecell:cell atindexpath:indexpath]; return cell; } #pragma mark - table view delegate - (void)configurecell:(classtableviewcell *)cell atindexpath:(nsindexpath *)indexpath { classes *classid = [self.fetchedresultscontroller objectatindexpath:indexpath]; cell.classlabel.text = classid.classtitle; cell.periodlabel.text = classid.period; } #pragma mark - fetched results controller - (nsfetchedresultscontroller *)fetchedresultscontroller { if (_fetchedresultscontroller != nil) { return _fetchedresultscontroller; } nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; nsentitydescription *entity = [nsentitydescription entityforname:@"myclassesid" inmanagedobjectcontext:self.managedobjectcontext]; assert(self.managedobjectcontext); [fetchrequest setentity:entity]; [fetchrequest setfetchbatchsize:20]; nssortdescriptor *sortdescriptor = [[nssortdescriptor alloc] initwithkey:@"period" ascending:yes]; nsarray *sortdescriptors = [nsarray arraywithobjects:sortdescriptor, nil]; [fetchrequest setsortdescriptors:sortdescriptors]; nsfetchedresultscontroller *afetchedresultscontroller = [[nsfetchedresultscontroller alloc] initwithfetchrequest:fetchrequest managedobjectcontext:self.managedobjectcontext sectionnamekeypath:nil cachename:@"master"]; afetchedresultscontroller.delegate = self; self.fetchedresultscontroller = afetchedresultscontroller; nserror *error = nil; if (![self.fetchedresultscontroller performfetch:&error]) { /* replace implementation code handle error appropriately. abort() causes application generate crash log , terminate. should not use function in shipping application, although may useful during development. */ nslog(@"unresolved error %@, %@", error, [error userinfo]); abort(); } return _fetchedresultscontroller; } - (void)controllerwillchangecontent:(nsfetchedresultscontroller *)controller { [self.tableview beginupdates]; } - (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:uitableviewrowanimationfade]; break; case nsfetchedresultschangedelete: [self.tableview deletesections:[nsindexset indexsetwithindex:sectionindex] withrowanimation:uitableviewrowanimationfade]; break; } } - (void)controller:(nsfetchedresultscontroller *)controller didchangeobject:(id)anobject atindexpath:(nsindexpath *)indexpath forchangetype:(nsfetchedresultschangetype)type newindexpath:(nsindexpath *)newindexpath { uitableview *tableview = self.tableview; switch(type) { case nsfetchedresultschangeinsert: [tableview insertrowsatindexpaths:[nsarray arraywithobject:newindexpath] withrowanimation:uitableviewrowanimationfade]; break; case nsfetchedresultschangedelete: [tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade]; break; case nsfetchedresultschangeupdate: [self configurecell:[tableview cellforrowatindexpath:indexpath] atindexpath:indexpath]; break; case nsfetchedresultschangemove: [tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade]; [tableview insertrowsatindexpaths:[nsarray arraywithobject:newindexpath] withrowanimation:uitableviewrowanimationfade]; break; } } - (void)controllerdidchangecontent:(nsfetchedresultscontroller *)controller { [self.tableview endupdates]; } - (bool)tableview:(uitableview *)tableview caneditrowatindexpath:(nsindexpath *)indexpath { // return no if not want specified item editable. return yes; } - (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath { nsmanagedobjectcontext *context = [self managedobjectcontext]; if (editingstyle == uitableviewcelleditingstyledelete) { // delete object database [context deleteobject:[self.classes objectatindex:indexpath.row]]; nserror *error = nil; if (![context save:&error]) { nslog(@"can't delete! %@ %@", error, [error localizeddescription]); return; } // remove device table view [self.classes removeobjectatindex:indexpath.row]; [self.tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade]; } } /* // override support conditional editing of table view. - (bool)tableview:(uitableview *)tableview caneditrowatindexpath:(nsindexpath *)indexpath { // return no if not want specified item editable. return yes; } */ /* // override support editing table view. - (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath { if (editingstyle == uitableviewcelleditingstyledelete) { // delete row data source [tableview deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade]; } else if (editingstyle == uitableviewcelleditingstyleinsert) { // create new instance of appropriate class, insert array, , add new row table view } } */ /* // override support rearranging table view. - (void)tableview:(uitableview *)tableview moverowatindexpath:(nsindexpath *)fromindexpath toindexpath:(nsindexpath *)toindexpath { } */ /* // override support conditional rearranging of table view. - (bool)tableview:(uitableview *)tableview canmoverowatindexpath:(nsindexpath *)indexpath { // return no if not want item re-orderable. return yes; } */ /* #pragma mark - navigation // in story board-based application, want little preparation before navigation - (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { // new view controller using [segue destinationviewcontroller]. // pass selected object new view controller. } */ @end
the code in receiving error:
line 251:
[context deleteobject:[self.classes objectatindex:indexpath.row]];
line 260:
[self.classes removeobjectatindex:indexpath.row];
first of all, remove #import "classes.m". second, assume self.classes array, raises 2 questions:
- if class named
classes
, why variable namedclasses
well? rename variablevclasses
oraclasses
orclasses
. also, make sureself.classes
(or in yourcaseself.classes
) being initialized properly. debug! - why using array
classes
objects, when havensfetchedresultscontroller
setup? should useself.fetchedresultscontroller
instead.
hope helps.
Comments
Post a Comment