为什么setEditing:animated:和insertRowsAtIndexPaths:导致这个奇怪的编辑风格动画?

Rya*_*yan 7 uitableview ios

我有一个UITableView我正在切换setEditing:animated:的东西,允许用户插入和删除行.当打开编辑时,我希望将新insert new item行添加到表中,然后我希望编辑控件以正常方式进行动画处理.我不希望新insert new item行使用类似淡入淡出的东西自行制作动画.我希望它只是出现,然后像任何现有的表数据源行一样滑入.

以下是我当前代码所发生的情况(点击查看大图):

顶行做了我想要的 - 它只是滑过,删除图标淡入.当它消失时,删除图标淡出,行再次展开.

第二行是我自己添加到表中的非数据源行.在出现时,它根本没有动画.插入图标和行一次显示,不会滑入.当它消失时,行会很好地展开,但加号图标会随之滑动.动画正在发生整个行,而不是加号图标,然后单独行,如第一行.

这是我的代码的快速破坏,但我认为提供类文件的链接可能会更好.

按下工具栏上的编辑按钮时,我调用我的UIViewController方法setEditing:animated:.在这种方法中,我做了以下......

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {

    [super setEditing:editing animated:animated];

    // keep the table in the same editing mode
    [_table setEditing:editing animated:animated];

    if (editing) {

        [_table insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_channels.count inSection:0]] withRowAnimation:UITableViewRowAnimationNone];

    } else {

        [_table deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_channels.count inSection:0]] withRowAnimation:UITableViewRowAnimationNone];

    }

}
Run Code Online (Sandbox Code Playgroud)

这是插入动画发生的地方.我已经尝试在[_table beginUpdate]和endUpdate中包装整个内容,以及只插入行.似乎都没有产生我想要的干净动画.

我可能会缺少什么想法?整个代码文件在这里:

https://github.com/ryancole/pound-client/blob/master/pound-client/controllers/ChannelListViewController.m#L106-L127

Kry*_*ski 2

super 调用执行滑动“编辑”动画,因此如果您在其后面插入某些内容,它将不会参与其中。您想要做的是在该调用之前插入行并删除之后的行。您还需要使用不同的布尔值来跟踪行。

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {

    _amEditing = editing;

    if (editing) {

        [_table insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_channels.count inSection:0]] withRowAnimation:UITableViewRowAnimationNone];

    } 

    [super setEditing:editing animated:animated];

    // keep the table in the same editing mode
    [self.view setEditing:editing animated:animated];

    if (!editing)
    {
        [_table deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_channels.count inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    }


}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _amEditing ? _channels.count + 1 : _channels.count;
}
Run Code Online (Sandbox Code Playgroud)

更新:

倒数第二行的图标仍然有一个奇怪的动画。要解决这个问题,您可以添加删除延迟。

    if (!editing)
    {
        [self performSelector:@selector(deleteLastRow) withObject:nil afterDelay:0.25];
    }

-(void) deleteLastRow
{
    [self.view deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_objects.count inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}
Run Code Online (Sandbox Code Playgroud)