tableview删除最后一行
问题描述:
我已经做了很多小时的研究在stackoverflow /谷歌,并没有研究过类似问题的几个帖子。然而,他们没有一个解决我所遇到的特殊问题。tableview删除最后一行
简而言之,我有一个NSMutableDictionay(-ies)的NSMutableArray。每个字典都包含一个NSMutableArray。当我删除数组中的最后一项时,我也想删除字典。在表格中,我想删除该部分的最后一行时删除该部分。
我确定数据源是一致的。
这篇文章在这里:UITableView: deleting sections with animation建议直接删除该部分,如果要删除的行是最后一行。
如果该部分是我的根数组中的最后一部分,则可以正常工作。
我发现的错误是,如果部分不是最后一部分,它后面的部分将代替它。当系统绘制删除动画时,它会抛出NSInternalInconsistencyException
。
说,我有部分0和部分1,都与一行。当我删除indexPath(0,0),我检测部分0应该去,而不是因为它只有一行,然后我删除部分0在commitEditingStyle
由于用户删除部分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];
答
如果你想删除的字典从阵列中,其作为下标变量对象,那么你需要这一条线上。
[yourObject removeObjectAtIndex:indexPath.section];
,然后通过这条线
[yourTable reloadData];
,因为现在你的表取决于该数据源,然后它会自动从表中删除重装该表。
狮子座你找到了这个问题的解决方案,你保留删除动画?或者你用reloadData方法satyed? – lukewar 2011-10-30 20:51:21