tableview删除部分的最后一行

leo*_*leo 2 iphone cocoa-touch uitableview ios

我已经花了很多时间研究stackoverflow/google并且研究过几个关于类似问题的帖子.然而,他们都没有解决我遇到的特殊问题.

简而言之,我有一个NSMutableArray的NSMutableDictionay(-ies).每个字典都包含一个NSMutableArray.当我删除数组中的最后一项时,我也想删除字典.在tableview文字中,我想在删除该部分的最后一行时删除该部分.

我确信数据源是一致的.

这篇文章:UITableView:删除带动画的部分建议如果要删除的行是最后一行,则直接删除该部分.

如果该部分是我的根数组中的最后一部分,这可以正常工作.

我发现的错误是,如果该部分不是最后一部分,那么它之后的部分将取代它.当系统正在绘制删除动画时,它会抛出一个NSInternalInconsistencyException.

说,我有第0部分和第1部分,都有一行.当我删除indexPath(0,0)时,我检测到0应该去,因为它只有一行,然后我删除0中的0commitEditingStyle

抛出异常是因为用户删除了第0节中的第0行,因此第0节中的结果行数应该从1更改为0.但是,删除后,旧的第1节现在变为第0节,它将返回行数第0节是1.

这里有什么建议吗?或者我错过了一些非常简单的事情?

我在commitEditingSytle中的代码:

[tableView beginUpdates];
[... remove the  item from array of this dictionary...]
// if last row in section, remove the section
NSNumber *section = [NSNumber numberWithUnsignedInteger: indexPath.section];
if ([[FoodTypes subtypes:section] count] == 0) 
{
    [...remove the dictionary from root array...]

    [tableView deleteSections: [NSIndexSet indexSetWithIndex: indexPath.section] withRowAnimation:YES];
}
else 
{
    // otherwise, remove the row only
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)

Ish*_*shu 7

如果要从数组中删除作为下标变量的对象的字典,则需要使用此单行.

[yourObject removeObjectAtIndex:indexPath.section];
Run Code Online (Sandbox Code Playgroud)

然后通过这一行重新加载表

[yourTable reloadData];
Run Code Online (Sandbox Code Playgroud)

现在,因为您的表依赖于此数据源,所以它会自动从表中删除.