【发布时间】:2011-05-05 02:39:01
【问题描述】:
我在 stackoverflow/google 上做了很多小时的研究,并研究了几篇关于类似问题的帖子。然而,他们都没有解决我遇到的特定问题。
简而言之,我有一个 NSMutableDictionay(-ies) 的 NSMutableArray。每个字典都包含一个 NSMutableArray。当我删除数组中的最后一项时,我也想删除字典。用tableview的话来说,我想在删除该部分的最后一行时删除该部分。
我确定数据源是一致的。
这里的帖子:UITableView: deleting sections with animation 建议如果要删除的行是最后一行,则直接删除该部分。
如果该部分是我的根数组中的最后一个部分,这可以正常工作。
我发现的错误是,如果部分不是最后一个部分,则后面的部分将取代它。系统在绘制删除动画时,会抛出NSInternalInconsistencyException。
比如说,我有第 0 节和第 1 节,都只有一行。当我删除 indexPath (0,0) 时,我检测到应该改为第 0 节,因为它只有一行,然后我删除 commitEditingStyle 中的第 0 节@
抛出异常是因为用户删除了 Section 0 中的 Row 0,所以 Section 0 中的结果行数应该从 1 变为 0。但是,删除之后,旧的 Section 1 现在变成了 Section 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];
【问题讨论】:
-
Leo 你有没有找到解决这个问题的方法,在哪里保留删除动画?或者您对 reloadData 方法感到满意?
标签: iphone cocoa-touch ios uitableview