ios - Invalid update: invalid number of rows in section -
i working on project using microsoft azure services
. in while deleting row getting 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 (2) must equal number of rows contained in section before update (2), 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).
code table load , delete row :
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 3; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { //medicine list if (section == 0) { nsarray *sectioninfo = [self.fetchedresultscontroller1 fetchedobjects]; return [sectioninfo count]; } //allergy list else if (section == 1) { nsarray *sectioninfo = [self.fetchedresultscontroller2 fetchedobjects]; return [sectioninfo count]; } //notes list else if (section == 2){ nsarray *sectioninfo = [self.fetchedresultscontroller3 fetchedobjects]; return [sectioninfo count]; } else return 0; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"mcell"; medicationtableviewcell *cell = (medicationtableviewcell *) [tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath]; // add utility buttons nsmutablearray *rightutilitybuttons = [nsmutablearray new]; [rightutilitybuttons sw_addutilitybuttonwithcolor: [uicolor colorwithred:1.0f green:0.231f blue:0.188 alpha:1.0f] title:@"delete"]; switch (indexpath.section) { case 0: { nsmanagedobject *item = [self.fetchedresultscontroller1 objectatindexpath:indexpath]; cell.textlabel.text = [item valueforkey:@"name"]; break; } case 1: { nsmanagedobject *item = [self.fetchedresultscontroller2 objectatindexpath:[nsindexpath indexpathforrow:indexpath.row insection:0]]; cell.textlabel.text = [item valueforkey:@"name"]; break; } case 2: { nsmanagedobject *item = [self.fetchedresultscontroller3 objectatindexpath: [nsindexpath indexpathforrow:indexpath.row insection:0]]; cell.textlabel.text = [item valueforkey:@"title"]; break; } default: break; } cell.rightutilitybuttons = rightutilitybuttons; cell.delegate = self; return cell; } - (void)swipeabletableviewcell:(swtableviewcell *)cell didtriggerrightutilitybuttonwithindex:(nsinteger)index { // delete button pressed nsindexpath *cellindexpath = [self.medicationstableview indexpathforcell:cell]; //fetchingg data local store first nsmanagedobject *item = [self.fetchedresultscontroller1 objectatindexpath:[nsindexpath indexpathforrow:cellindexpath.row insection:0]]; nsmutablearray *keys = [[nsmutablearray alloc] initwithobjects:@"id", @"name", @"userid", nil]; nsmutablearray *values = [[nsmutablearray alloc] initwithobjects: [item valueforkey:@"id"], [item valueforkey:@"name"], [item valueforkey:@"userid"], nil]; //creating dictionary of data nsdictionary *dict = [[nsdictionary alloc] initwithobjects:values forkeys:keys]; //calling delete fucntion [self.authservice deleteitem:self.synctable withdict:dict completion:^{ //removing row table view [self.medicationstableview reloaddata]; [self.medicationstableview deleterowsatindexpaths:@[cellindexpath] withrowanimation:uitableviewrowanimationright]; }]; break; }
please tell going wrong. in advcance!!!!
in - (void)swipeabletableviewcell: didtriggerrightutilitybuttonwithindex:
you need remove either
[self.medicationstableview reloaddata];
or
[self.medicationstableview deleterowsatindexpaths:@[cellindexpath] withrowanimation:uitableviewrowanimationright];
because on first line when reloaddata
called it's reload tableview new datasource , again calling deleterowsatindexpaths
tried delete rows removed calling reloaddata
earlier.