【发布时间】:2011-03-06 14:24:13
【问题描述】:
我是 iPhone 应用程序开发的新手。所以请和我一起放轻松:)
当我得到这个错误时,我试图从 UItableView 中删除行,我不明白为什么
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).'
这是我删除项目方法的代码
-(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
}
这是 UITableView 的提交编辑风格
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[appDelegate deleteItem:indexPath];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
谁能告诉我我做错了什么。据我了解,该部分中的行数未更新。我说的对吗??
提前致谢。
【问题讨论】:
-
删除表格中的最后一个元素时不会出现此异常。我错过了什么吗? :(
标签: iphone objective-c xcode uitableview