0
votes
- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
    if(editingStyle == UITableViewCellEditingStyleDelete) {

        //Get the object to delete from the array.
        NSLog(@"%@",[[_arrayForTable valueForKey:@"IDS"] objectAtIndex:[indexPath row]] );

        NSInteger myInteger = [[[_arrayForTable valueForKey:@"IDS"] objectAtIndex:[indexPath row]] integerValue];
      NSNumber *selRow = [NSNumber numberWithInt:myInteger];
        NSLog(@"%@",selRow);
        [self removeCell:selRow]; // this is working perfectly it gets remove from database

        //Delete the object from the table.
        // app get crash here crash report is pested below 
        [self.table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }
}

- (void) removeCell:(NSNumber *)selRow{

       NSLog(@"_arrayForTable%@",_arrayForTable);
 NSInteger myInteger = [selRow integerValue];

    [_arrayForTable removeObjectAtIndex:myInteger];

    NSLog(@"_arrayForTable%@",_arrayForTable);



    if(deleteStmt == nil) {
        const char *sql = "delete from PersonNamesAndBirthDates where ID = ?";
        NSLog(@"%s",sql);
        if(sqlite3_prepare_v2(database1, sql, -1, &deleteStmt, NULL) != SQLITE_OK)
            NSAssert1(0, @"Error while creating delete statement. '%s'", sqlite3_errmsg(database1));
    }
    NSLog(@"%d",myInteger);

    //When binding parameters, index starts from 1 and not zero.
    sqlite3_bind_int(deleteStmt, 1, myInteger );

    if (SQLITE_DONE != sqlite3_step(deleteStmt)) 
        NSAssert1(0, @"Error while deleting. '%s'", sqlite3_errmsg(database1));

    sqlite3_reset(deleteStmt);
}

* Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1046 2013-04-08 17:01:13.069 birthdate reminder 2[583:15803] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (12) must be equal to the number of rows contained in that section before the update (12), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

please suggest something

my numberofrow method is below

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.


    return _arrayForTable.count;

}

app is not crashing everytime sometime works sometime crash

3
paste your numnerOfRows method ?Manu
My guess is that your assumption about removeCell:, that it is working perfectly, might be the problem. The row might be deleted from your database, but have you checked your variable _arrayForTable? It seems, it's still in there.fguchelaar
it gets deleted. but sometime it shows count right sometime wrong. when app get launch again then _arrayForTable is correct i dont understand whyPrakash Desai

3 Answers

1
votes

Ok, so as I see it, you are getting the ID of a specific row with:

NSInteger myInteger = 
    [[[_arrayForTable valueForKey:@"IDS"] objectAtIndex:[indexPath row]] integerValue];
NSNumber *selRow = [NSNumber numberWithInt:myInteger];

And then, you're using the ID of that specific row, as the index to remove it from the array.

NSInteger myInteger = [selRow integerValue];
[_arrayForTable removeObjectAtIndex:myInteger];

That's where things go wrong. If the array has the same order as your TableView, you need to remove indexPath.row from it. The ID is necessary for your database.

0
votes

Try this:

- (void)tableView:(UITableView *)tv commitEditingStyle :(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if(editingStyle == UITableViewCellEditingStyleDelete) {

        //Get the object to delete from the array.
        NSLog(@"%@",[[_arrayForTable valueForKey:@"IDS"] objectAtIndex:[indexPath row]] );

        NSInteger myInteger = [[[_arrayForTable valueForKey:@"IDS"] objectAtIndex:[indexPath row]] integerValue];
        NSNumber *selRow = [NSNumber numberWithInt:myInteger];
        NSLog(@"%@",selRow);

        [self.table beginUpdates];        

        [self removeCell:selRow]; // this is working perfectly it gets remove from database

        //Delete the object from the table.
        // app get crash here crash report is pested below 
        [self.table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        [self.table endUpdates];

    }
}

Notice the [self.table beginUpdates]; and [self.table endUpdates]; around the lines of code which alter the tableview.

0
votes

IT"S WORKING at and of your commitEditingStyle method add this line

[TableViewForFirstView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];