ios - How to manage Core Data to fulfill appropriate section? -
i have 1 table view controller (cardtableviewcontroller) 3 sections (english, chinese, spanish) , view controller (cardeditviewcontroller) can add new items in cards: e.g. can add "lesson 6" english. problem use core data, when create new item, appears not in english elsewhere (in english, spanish , chinese – same item everywhere).
how can create save in appropriate section? it's kind of relationship.
cardtableviewcontroller.m
#import "cardtableviewcontroller.h" @import coredata; @interface cardtableviewcontroller () @property (strong) nsmutablearray *collections; @end @implementation cardtableviewcontroller - (nsmanagedobjectcontext *)managedobjectcontext { nsmanagedobjectcontext *context = nil; id delegate = [[uiapplication sharedapplication] delegate]; if ([delegate performselector:@selector(managedobjectcontext)]) { context = [delegate managedobjectcontext]; } return context; } - (void)viewdidappear:(bool)animated { [super viewdidappear:animated]; nsmanagedobjectcontext *managedobjectcontext = [self managedobjectcontext]; nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] initwithentityname:@"collection"]; self.collections = [[managedobjectcontext executefetchrequest:fetchrequest error:nil] mutablecopy]; [self.tableview reloaddata]; } - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return self.collections.count; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"cell" forindexpath:indexpath]; nsmanagedobject *collection = [self.collections objectatindex:indexpath.row]; [cell.textlabel settext:[nsstring stringwithformat:@"%@", [collection valueforkey:@"name"]]]; return cell; } - (bool)tableview:(uitableview *)tableview caneditrowatindexpath:(nsindexpath *)indexpath { return yes; } - (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath { nsmanagedobjectcontext *context = [self managedobjectcontext]; if (editingstyle == uitableviewcelleditingstyledelete) { [context deleteobject:[self.collections objectatindex:indexpath.row]]; nserror *error = nil; if (![context save:&error]) { nslog(@"can't delete! %@ %@", error, [error localizeddescription]); return; } [self.collections removeobjectatindex:indexpath.row]; [self.tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade]; } } @end
cardeditviewcontroller.h
#import <uikit/uikit.h> @interface cardeditviewcontroller : uiviewcontroller @property (weak, nonatomic) iboutlet uitextfield *namecommon; - (ibaction)save:(id)sender; - (ibaction)cancel:(id)sender; @end
cardeditviewcontroller.m
#import "cardeditviewcontroller.h" @import coredata; @interface cardeditviewcontroller () @end @implementation cardeditviewcontroller - (nsmanagedobjectcontext *)managedobjectcontext { nsmanagedobjectcontext *context = nil; id delegate = [[uiapplication sharedapplication] delegate]; if ([delegate performselector:@selector(managedobjectcontext)]) { context = [delegate managedobjectcontext]; } return context; } - (ibaction)cancel:(id)sender { [self.navigationcontroller popviewcontrolleranimated:yes]; } - (ibaction)save:(id)sender { nsmanagedobjectcontext *context = [self managedobjectcontext]; nsmanagedobject *newcollection = [nsentitydescription insertnewobjectforentityforname:@"collection" inmanagedobjectcontext:context]; [newcollection setvalue:self.namecommon.text forkey:@"name"]; nserror *error = nil; if (![context save:&error]) { nslog(@"can't save! %@ %@", error, [error localizeddescription]); } [self.navigationcontroller popviewcontrolleranimated:yes]; } @end
this not how done. trying solve problems have been solved you. instead, should use nsfetchedresultscontroller
populate table view core data.
create new project (e.g. master-detail) , check "core data" option see how done. can copy paste fetched results controller code app without many modifications.
Comments
Post a Comment