Iphone UITableView删除行错误

Iphone UITableView删除行错误,iphone,objective-c,xcode,uitableview,Iphone,Objective C,Xcode,Uitableview,我是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

我是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];
  }      
}
有人能告诉我我做错了什么吗。据我所知,该节中的行数没有更新。我说的对吗


提前谢谢

以我的谦虚观点,在尝试了同样的方法之后,表似乎仍然认为行数没有改变,因此我将行隐藏起来,看起来它对我有效。

正如苹果iOS表视图编程指南中所述,删除顺序是相关的:

首先从表中删除该项; 其次,将其从模型中删除

因此,您必须切换两个删除命令:


[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[appDelegate deleteItem:indexPath];

在重新审视代码之后,我自己找到了解决方案。问题出在

readTable()
在重新读取以前的数组之前未清空该数组。我用了

[list removeAllObjects]

方法将其清空。它终于起作用了

我没有看到[表重载数据];它有用吗?通常是必需的。

删除表中的最后一个元素时,不会出现此异常。我是不是遗漏了什么(