objective c - Delete cell from UITableView not from Array -
i learn how work coredata , table view controller. learn book , there using function display content of tableviewcontrooler.
-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{ if(debug == 1){ nslog(@"running %@ '%@'",self.class, nsstringfromselector(_cmd)); } static nsstring *cellindetifier = @"class cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellindetifier forindexpath:indexpath]; cell.accessorytype = uitableviewcellaccessorydetailbutton; schoolclass *class = [self.frc objectatindexpath:indexpath]; nsmutablestring *title = [nsmutablestring stringwithformat:@"%@", class.name]; [title replaceoccurrencesofstring:@"(null)" withstring:@"" options:0 range:nsmakerange(0, [title length])]; cell.textlabel.text = title; return cell; } now delete record(cell) table view controller , coredata. don't know simplest way, found this:
i have 1 button "delete" , button has iba action.
-(ibaction)deleteclassaction:(id)sender{ [self.tableview setediting:yes animated:yes]; } when press button see this: http://i.stack.imgur.com/3vayp.png
now need delete selected cell. found code , prepared skeleton deleting. need advice in 2 things.
i don't know how id of deleting item , how write code filter - viz. comment: // code filters object has been marked deleting cell tableview
i don't know how delete cell tableview. found solution, under comment, has error:
terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: 'invalid update: invalid number of rows in section 0. number of rows contained in existing section after update (10) must equal number of rows contained in section before update (10), plus or minus number of rows inserted or deleted section (0 inserted, 1 deleted) , plus or minus number of rows moved or out of section (0 moved in, 0 moved out).'
here code comments.
-(void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath{ // deleting data coredata database coredatahelper *cdh = [(appdelegate *) [[uiapplication sharedapplication] delegate] cdh]; nsfetchrequest *request = [nsfetchrequest fetchrequestwithentityname:@"user"]; nspredicate *filter = // code filters object has been marked deleting cell tableview" [request setpredicate:filter]; nsarray *fetchedobjects = [cdh.context executefetchrequest:request error:nil]; for(schoolclass *class in fetchedobjects){ [_coredatahelper.context deleteobject:class]; } // deleting cell tableview if (editingstyle == uitableviewcelleditingstyledelete) { [self.tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade]; } [self.tableview reloaddata]; }
you need delete object nsfetchedresultscontroller. might want save nsmanagedobjectcontext. fetched results controller update table view assuming you've implemented delegate methods described in docs.
-(void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath{ // deleting cell fetched results contoller if (editingstyle == uitableviewcelleditingstyledelete) { schoolclass *class = [self.frc objectatindexpath:indexpath]; [[class managedobjectcontext] deleteobject:class]; } }
Comments
Post a Comment