0

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.

1
  • This exception does not appear when i am deleting the final element in the table. Am i missing something ?? :( Commented Mar 6, 2011 at 15:58

4 Answers 4

1

As stated in the Apple's Table View Programming Guide for iOS the deletion order is relevant:

first delete the item from the table; second delete it from the model.

So you have to switch the two delete commands:


[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[appDelegate deleteItem:indexPath];
Sign up to request clarification or add additional context in comments.

Comments

0

In my modest opinion and after trying to the same thing, looks like the table still believes the number of rows didn't change, so I make the row hidden and looks like it worked for me.

Comments

0

After some re-looking at the code, figured out the solution myself. The problem was with the the

readTable()

where the older array was not emptied before reading it again. I used the

[list removeAllObjects]

method to empty it. It worked finally. :)

Comments

0

I don't see a [table reloadData]; does it work? Usually it is required.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.