UITableView删除行错误 [英] UITableView Deleteing row error

查看:51
本文介绍了UITableView删除行错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iPhone应用程序开发的新手.所以,请和我一起轻松:)

I am a newbie to iPhone app development. So please go easy with me :)

当我收到此错误时,我试图从UItableView中实现删除行,我无法理解为什么

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

}

这是UITableView的提交编辑样式

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 ??

先谢谢了.

推荐答案

重新查看代码后,自己找出解决方案.问题出在

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. :)

这篇关于UITableView删除行错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆