i have storyboard core data project 1 entity named "password" , 4 string attributes, data connected table view fecthedresultscontroller , sections set 1 of attributes calld "type".
when launch app every thing o.k, can add, update , delete , nothing wrong. when go out of app home screen , come nothing happens. problem when close app multitasking menu , re launch it, empty cells in same amount of ones. grouped @ top no section.
what doing wrong?
insertviewcontroller.m
-(ibaction)insertpassword:(id)sender { nsinteger row; nsstring*subtypeselectd=[[nsstring alloc]init]; row = [pickersubjects selectedrowincomponent:0]; subtypeselectd = [arrsubsubjects objectatindex:row]; nsinteger row2; nsstring*typeselectd=[[nsstring alloc]init]; row2 = [pickersubjects selectedrowincomponent:1]; typeselectd = [arrsubject objectatindex:row2]; // self.insertpassword.desc=self.txtdesc.text; // self.insertpassword.username=self.txtusername.text; // self.insertpassword.password=self.txtpassword.text; // self.insertpassword.type=typeselectd; // self.insertpassword.subtype=subtypeselectd; // nslog(@"insertpassword"); // [self.delegete addpasswordviewcontrollerdidsave]; appdelegate *delegate = [uiapplication sharedapplication].delegate; nsmanagedobjectcontext *addviewcontrollerlocalcontext = delegate.managedobjectcontext; nsmanagedobject*password=[nsentitydescription insertnewobjectforentityforname:@"password" inmanagedobjectcontext:addviewcontrollerlocalcontext]; [password setvalue:self.txtdesc.text forkey:@"desc"]; [password setvalue:self.txtusername.text forkey:@"username"]; [password setvalue:self.txtpassword.text forkey:@"password"]; [password setvalue:subtypeselectd forkey:@"subtype"]; [password setvalue:typeselectd forkey:@"type"]; nserror*error; if(![addviewcontrollerlocalcontext save:&error]) nslog(@"input %@",error); else nslog(@"saved"); [self.navigationcontroller popviewcontrolleranimated:yes]; }
and mainviewcontroller.m
-(void)addpasswordviewcontrollerdidsave { nsmanagedobjectcontext*context=self.manageobjectcontext; nserror*error; if(![context save:&error]) nslog(@"error : %@",error); nslog(@"addpasswordviewcontrollerdidsave"); [self.navigationcontroller popviewcontrolleranimated:yes]; } -(void)addpasswordviewcontrollerdiddelete:(password *)passwordtodelete { nslog(@"b"); nsmanagedobjectcontext*context=self.manageobjectcontext; [context deleteobject:passwordtodelete]; [self.navigationcontroller popviewcontrolleranimated:yes]; } -(void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if([[segue identifier]isequaltostring:@"addpassword"]) { insertviewcontroller*ipvc=(insertviewcontroller*)[segue destinationviewcontroller]; password*p=(password*)[nsentitydescription insertnewobjectforentityforname:@"password" inmanagedobjectcontext:[self manageobjectcontext]]; // ipvc.delegete=self; ipvc.insertpassword=p; } if([[segue identifier]isequaltostring:@"updatepassword"]) { updateviewcontroller*uvc=(updateviewcontroller*)[segue destinationviewcontroller]; nsindexpath*indexpath=[self.tableview indexpathforselectedrow]; password*p=[self.fecthedresultscontroller objectatindexpath:indexpath]; uvc.updatepassword=p; } } //-------------------------------------------------------------table------------ -(nsfetchedresultscontroller*)fecthedresultscontroller { if(fecthedresultscontroller!=nil) return fecthedresultscontroller; nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; nsentitydescription *entity = [nsentitydescription entityforname:@"password" inmanagedobjectcontext:[self manageobjectcontext]]; [fetchrequest setentity:entity]; nssortdescriptor *sortdescriptor = [[nssortdescriptor alloc] initwithkey:@"type" ascending:yes]; nsarray *sortdescriptors = [[nsarray alloc] initwithobjects:sortdescriptor, nil]; [fetchrequest setsortdescriptors:sortdescriptors]; fecthedresultscontroller=[[nsfetchedresultscontroller alloc]initwithfetchrequest:fetchrequest managedobjectcontext:[self manageobjectcontext] sectionnamekeypath:@"type" cachename:nil]; fecthedresultscontroller.delegate=self; return fecthedresultscontroller; } -(void)controllerwillchangecontent:(nsfetchedresultscontroller *)controller { [self.tableview beginupdates]; [self.tableview reloaddata]; } -(void)controllerdidchangecontent:(nsfetchedresultscontroller *)controller { [self.tableview endupdates]; } -(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:{ password*p=[self.fecthedresultscontroller objectatindexpath:indexpath]; uitableviewcell*cell=[tableview cellforrowatindexpath:indexpath]; cell.textlabel.text=p.username; cell.detailtextlabel.text=p.password; // cell.imageview.image=[uiimage imagewithdata: p.photodata]; } break; case nsfetchedresultschangemove: [tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade]; [tableview insertrowsatindexpaths:[nsarray arraywithobject:newindexpath] withrowanimation:uitableviewrowanimationfade]; break; default: break; }} -(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]; default: break; } } -(nsstring*)tableview:(uitableview *)tableview titleforheaderinsection: (nsinteger)section { return [[[self.fecthedresultscontroller sections]objectatindex:section]name]; } - (void)tableview:(uitableview *)tableview commiteditingstyle: (uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath { if (editingstyle == uitableviewcelleditingstyledelete) { nsmanagedobjectcontext*context=[self manageobjectcontext]; password*passwordtodelete=[self.fecthedresultscontroller objectatindexpath:indexpath]; [context deleteobject:passwordtodelete]; nserror*error; if(![context save:&error]) nslog(@"error"); } } //-------------------------------------------------------------table-end----------- - (id)initwithstyle:(uitableviewstyle)style { self = [super initwithstyle:style]; if (self) { // custom initialization } return self; } -(void)viewwillapear { } - (void)viewdidload { [super viewdidload]; nserror*error; if(![[self fecthedresultscontroller]performfetch:&error]) { nslog(@"error"); abort(); } // 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; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } #pragma mark - table view data source - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { //#warning potentially incomplete method implementation. // return number of sections. nslog(@"%i",[[self.fecthedresultscontroller sections]count]); return [[self.fecthedresultscontroller sections]count]; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { //#warning incomplete method implementation. // return number of rows in section. id<nsfetchedresultssectioninfo>secinfo=[[self.fecthedresultscontroller sections]objectatindex:section]; return [secinfo numberofobjects]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath]; // configure cell... nslog(@"cell"); password*p=[self.fecthedresultscontroller objectatindexpath:indexpath]; cell.textlabel.text=p.username; cell.detailtextlabel.text=p.password; // if(p.photodata!=nil) // cell.imageview.image=[uiimage imagewithdata: p.photodata]; return cell; } - (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { return 60; }
you have typo in line in cellforrowatindexpath
method:
it should fetched resultscontroller:
password*p=[self.fecthedresultscontroller objectatindexpath:indexpath];
and again in numberofsectionintableview
:
nslog(@"%i",[[self.fecthedresultscontroller sections]count]); return [[self.fecthedresultscontroller sections]count];
and possibly in other methods.
Comments
Post a Comment