好吧,我被困住了。这是我之前的一篇文章的延伸。这是我想要做的。
我在导航栏上有一个编辑按钮,按下该按钮时会在我的一个部分表视图的开头添加一个单元格。此单元格的用途是否允许使用向表中添加新数据;因此它的编辑风格是插入。表格中剩余的单元格配置为删除编辑样式。
这是我的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 中添加重新加载表格视图单元格的调用,单元格将重新编制索引,但现在我的表格视图不再具有动画效果。
我想要我的蛋糕,也吃掉它。有没有另一种方法来完成我想做的事情并保持动画?
- 约翰