【发布时间】:2011-11-23 01:07:14
【问题描述】:
好吧,我卡住了。这是我之前的一篇文章的延伸。这就是我想要做的。
我在导航栏上有一个编辑按钮,按下该按钮时会在我的一个部分表视图的开头添加一个单元格。此单元格的用途是否允许使用向表中添加新数据;因此它的编辑风格是插入。表格中剩余的单元格配置为删除编辑样式。
这是我的setediting方法:
- (IBAction) setEditing:(BOOL)isEditing animated:(BOOL)isAnimated
{
[super setEditing:isEditing animated:isAnimated];
// We have to pass this to tableView to put it into editing mode.
[self.tableView setEditing:isEditing animated:isAnimated];
// When editing is begun, we are adding and "add..." cell at row 0.
// When editing is complete, we need to remove the "add..." cell at row 0.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSArray* path = [NSArray arrayWithObject:indexPath];
// fill paths of insertion rows here
[self.tableView beginUpdates];
if( isEditing )
[self.tableView insertRowsAtIndexPaths:path withRowAnimation:UITableViewRowAnimationBottom];
else
[self.tableView deleteRowsAtIndexPaths:path withRowAnimation:UITableViewRowAnimationBottom];
[self.tableView endUpdates];
// We nee to reload the table so that the existing table items will be properly
// indexed with the addition/removal of the the "add..." cell
[self.tableView reloadData];
}
我在我的代码中考虑了这个额外的单元格,除了我现在有两个索引路径 = [0,0] - 新单元格和表中旧的原始第一个单元格。如果我在 setEditing 中添加一个重新加载表格视图单元格的调用,单元格会被重新索引,但现在我的表格视图不再是动画。
我想要我的蛋糕,也吃掉它。还有其他方法可以完成我想做的事情并保持动画吗?
--约翰
【问题讨论】:
-
我已经看到了示例代码,除了在表格末尾添加了新单元格之外。不是我想要的 - 我想要它在表格的开头。
-
不清楚问题是什么以及您要完成什么。
-
以通讯录为例。当您编辑联系人时,会在“地址”部分的末尾添加带有“+”编辑样式的单元格“添加新地址”。我想做类似的事情,除了我想要在本节开头的新单元格。我可以通过在 indexPath [0,0] 处插入单元格来轻松完成此操作。但这给了我两个位于 [0,0] 的单元格(新单元格和 [0,0] 处的原始单元格)。我可以通过重新加载表格(重新索引单元格)来解决这个问题,除非我这样做时按下编辑按钮时表格不再动画。
-
我很好奇您使用的是什么类型的数据源?是数组还是fetchedresultcontroller,还是别的什么?
-
数据源只是一个数据对,就像标题和副标题一样。我只是使用地址簿作为我想要对单元格执行的操作的示例。
标签: ios tableview tableviewcell