2014-01-16 52 views
0

我试图设法从UITable中删除一行,该行是UIViewController的一部分。我使用导航栏中的Edit按钮。点击它将把表格行置于编辑模式。但是,当连续的删除按钮被按下时使用以下时,我得到一个错误...'Invalid update: invalid number of rows in section 0….ios从ViewController中的表中删除行

- (void)setEditing:(BOOL)editing animated:(BOOL)animated { 
[super setEditing:editing animated:animated]; 
[self.tableView setEditing:editing animated:YES]; 

} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     NSMutableArray *work_array = [NSMutableArray arrayWithArray:self.inputValues]; 
     [work_array removeObjectAtIndex:indexPath.row]; 
     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 

我怎么会错过吗?某种程度上,Apple文档似乎已过时。 谢谢

回答

2

问题很简单。从表中删除行之前,您没有正确更新数据模型。

你所要做的就是创建一些新的数组并从中删除一行。这没有意义。您需要更新其他数据源方法(如numberOfRowsInSection:)所使用的相同阵列。

+0

你好rmaddy,谢谢你的提示。我只是将附加数组取出并将其更改为我在数据模型中使用的数组。我太sl。了。谢谢! – JFS

1

您遇到的问题是您并未直接更新表格的数据源。你首先根据你的数据源创建一个名为work_array的全新数组(我假设它是self.inputValues),然后从中删除一个项目,然后尝试删除一行,但是你的tableView的数据源仍然包含该项目你打算删除。

所有你需要做的是确保self.inputValues是一个可变的数组,直接删除的对象的索引为数组,像这样:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [self.inputValues removeObjectAtIndex:indexPath.row]; 
     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 

我希望帮助!

+0

感谢您的回答,我发现我的错误与rmaddys提示。不管怎么说,还是要谢谢你! – JFS

+0

没问题,一定要接受他作为正确答案! – Mike