Iphone UITableview:-insertRowsAtIndexPaths:withRowAnimation:-未获取所有单元格的动画

Iphone UITableview:-insertRowsAtIndexPaths:withRowAnimation:-未获取所有单元格的动画,iphone,uitableview,insert,Iphone,Uitableview,Insert,在我的表视图中,我插入了一些行 [self.tableView beginUpdates]; [self.tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft]; [self.tableView endUpdates]; [self.tableView scrollToRowAtIndexPath:[arCells lastObject] atScrollPosition:UIT

在我的表视图中,我插入了一些行

[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];
[self.tableView scrollToRowAtIndexPath:[arCells lastObject] atScrollPosition:UITableViewScrollPositionBottom animated:YES];

我没有为所有单元格获取动画
UITableViewRowAnimationLeft
。假设我插入5行,我只为前2个单元格获得动画
UITableViewRowAnimationLeft
,其余单元格插入时没有动画。谁能告诉我为什么会这样?我做错了什么吗?

所以我们的目标是插入并定位内容,使所有插入的行都可见。只要插入的行比表本身短,这是可行的

滚动动画和插入似乎相互干扰。为了解决这个问题,让我们先进行滚动,因为文档提供了一个明确的钩子,当动画完成时,即委托方法
-(void)ScrollViewDiEndScrollingAnimation:(UIScrollView*)scrollView

解决方案如下所示:

// about to insert cells at arCells index paths
// first scroll so that the top is visible
NSIndexPath *firstNewIndexPath = [arCells objectAtIndex:0];
NSInteger previousRow = MAX(firstNewIndexPath.row-1, 0);
NSIndexPath *previousIndexPath = [NSIndexPath indexPathForRow:previousRow inSection:firstNewIndexPath.section];

// if the new rows are at the bottom, adjust the content inset so the scrolling can happen

if (firstNewIndexPath.row > [self.tableView numberOfRowsInSection:0) {
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, self.tableView.frame.size.height - 80, 0);  // 80 is just to illustrate, get a better row height from the table
}

[self.tableView scrollToRowAtIndexPath:previousIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

// there may be a better way to setup that scroll, not sure, but that should work.
现在我们有一个钩子来知道动画已经完成了。我们可以安全地插入

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {

    // hopefully you have those arCells in an instance variable already, otherwise
    // i think you'll need to create one to save state in between the two animations
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft];
    [self.tableView endUpdates];

    // restore the content inset
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}

另外两个处理是获得一个钩子来告诉我们行动画已经完成。这可能更好,因为这样我们就可以更好地了解滚动到哪里(如您的问题所示,滚动到新插入行的底部)。但是这些似乎都不一定能让我们知道动画已经完成。

只是一种预感:你能试着对scrollToRow进行注释,看看是否有相同的行为方式吗?是的,我进行了注释和测试。此时,当我在最后一个单元格后插入5行时,我无法看到动画。可见的行正确地获得了动画。对,所以我认为这是预期的行为。我认为问题在于,我们正在一起发布两个动画,影响相同的东西。这是一个比赛条件。让我检查文档,看看是否有一个钩子告诉你插入动画已经完成,然后我们可以开始滚动哦..好。。谢谢你,丹。。。我想要的是,如果要插入的行不可见,那么它应该向上移动以使其可见。我需要动画UITableViewRowAnimationLeft。你能告诉我这是可能的还是不可能的吗?是的。我认为这是可以做到的。我将在下面回答……再仔细想想,我认为当新细胞处于最底层时,它需要工作。滚动不会做我们想要的,因为新的单元格还没有出现。一个想法是使用contentInset。将编辑以进行说明。