2010-07-22 36 views
0

我试图通过滑动手指(如最近调用)来删除tableView上的单元格。我知道我需要实现:如何删除手指在iPhone上滑动的单元格

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  
{ 
    NSArray *index = [[NSArray alloc]initWithObjects:indexPath]; 
    [self.tableView deleteRowsAtIndexPaths:index withRowAnimation: UITableViewRowAnimationNone]; 
} 

但是,当我滑动手指,我得到这个功能和第二排,当我试图删除单元格的得到错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) 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).

有人可以帮忙吗?

回答

2

当你调用

[self.tableView deleteRowsAtIndexPaths:index withRowAnimation: UITableViewRowAnimationNone]; 

表视图会尝试重新加载,这反过来又调用委托方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

既然你已经删除一行,进行必要的动画才能正常工作,表格视图需要按照您删除的行数相同的行数进行更改deleteRowsAtIndexPaths:withRowAnimation:。您收到的错误消息试图解释依赖关系。这是说你在表视图有行,你删除 ...但是当表视图问你在删除后多少行了,你又说了一遍,而不是4-1 = 3它预计...然后它开始关闭本身大概是因为it did not want to live in a world where the laws of natural numbers could not explain reality

例如,如果您使用的是的NSArray *来填充您的tableView,你应该在同一时间进行删除该数组中的相应的对象,你在呼唤deleteRowsAtIndexPaths:withRowAnimation:

相关问题