I am a newbie to iPhone app development. So please go easy with me :)
I was trying to implement delete row from UItableView when i get this error, which i am not able understand why
Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (6) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'
Here is my code to delete item method
-(void)deleteItem:(NSIndexPath *)path{
Item *i = (Item *)[list objectAtIndex:path.row];
NSLog(@"Deleting item [%@]", i.iName);
int ret;
const char *sql = "delete from items where id = ?;";
if (!deleteStmt)
{ // build update statement
if ((ret=sqlite3_prepare_v2(db, sql, -1, &deleteStmt, NULL))!=SQLITE_OK)
{
NSAssert1(0, @"Error building statement to delete items [%s]", sqlite3_errmsg(db));
}
}
// bind values to statement
NSInteger n = i.iId;
sqlite3_bind_int(deleteStmt, 1, n);
// now execute sql statement
if ((ret=sqlite3_step(deleteStmt)) != SQLITE_DONE)
{
NSAssert1(0, @"Error deleting item [%s]", sqlite3_errmsg(db));
}
// now reset bound statement to original state
sqlite3_reset(deleteStmt);
[list removeObjectAtIndex:path.row]; // remove from table
[self readTable]; // refresh array
}
and this is the commitediting style of the UITableView
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[appDelegate deleteItem:indexPath];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
Can someone please tell me what i am doing wrong. From what i understand the number of rows in the section is not updated. Am i right ??
Thanks in advance.