ios - Delete Row from Table view -


i developing ios app. delete row in tableview , data load in core data in table view. when click delete button app crash. reason reason: invalid update: invalid number of rows in section 0. number of rows contained in existing section after update (18) must equal number of rows contained in section before update (18), 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)...

_fetchedobj nsarray fetch data core data

//code - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {      return _fetchedobj.count; }  - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {     static nsstring *myidentifier = @"myreuseidentifier";     uitableviewcell *cell = [mytable dequeuereusablecellwithidentifier:myidentifier];     if (cell == nil) {         cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault  reuseidentifier:myidentifier];     }     data = [_fetchedobj objectatindex:indexpath.row];     return cell; }  -(void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath*)indexpath {     if (editingstyle == uitableviewcelleditingstyledelete) {         [tableview beginupdates];         nsmanagedobject *managedobject = [_fetchedobj objectatindex:indexpath.row];         [self.managedobjectcontext deleteobject:managedobject];         [mytable deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade];         [mytable endupdates];     } } 

don't forget remove object datasource array?

try repopulate _fetchedobj because must configure model after deletion. more detail, read https://developer.apple.com/library/prerelease/ios/documentation/userexperience/conceptual/tableview_iphone/manageinsertdeleterow/manageinsertdeleterow.html

try this

-(void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath*)indexpath {     if (editingstyle == uitableviewcelleditingstyledelete) {         [tableview beginupdates];         nsmanagedobject *managedobject = [_fetchedobj objectatindex:indexpath.row];         [self.managedobjectcontext deleteobject:managedobject];         // insert line code          nsmutablearray *newa = [_fetchedobj mutablecopy ];         [newa removeobjectatindex: indexpath.row];         _fetchedobj = newa;          [mytable deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade];         [mytable endupdates];     } } 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -