Ios 如何将tableView平滑定位到最后插入的部分

Ios 如何将tableView平滑定位到最后插入的部分,ios,objective-c,uitableview,Ios,Objective C,Uitableview,编辑: 我发现这条线很长 我有一张桌子 我最初加载了5个部分 然后我向上滚动,当显示第0节和第0行时,我加载接下来的5节并使用 但是,我的问题是,tableView在插入后的第0节和第0行获得位置 但我想定位在插入的最后一个新节的最后一行(即上面代码中的第2节) 我在开始/结束更新的内部和外部都进行了尝试 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:2]; [self scrollToRowAtIndex

编辑:

我发现这条线很长

  • 我有一张桌子
  • 我最初加载了5个部分
  • 然后我向上滚动,当显示第0节和第0行时,我加载接下来的5节并使用
但是,我的问题是,tableView在插入后的第0节和第0行获得位置

但我想定位在插入的最后一个新节的最后一行(即上面代码中的第2节)

我在开始/结束更新的内部和外部都进行了尝试

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:2];
[self scrollToRowAtIndexPath:indexPath
            atScrollPosition:UITableViewScrollPositionBottom animated:YES];

但它做得很突然

插入节后,还可以使用

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:2];

[UIView animateWithDuration: 1.0
                  animations: ^{
                      [tableView scrollToRowAtIndexPath:indexPath
            atScrollPosition:UITableViewScrollPositionBottom animated:NO];
                  }
                  completion: ^(BOOL finished){
                  }
    ];
注意

1) 在
[self endUpdates]之后插入上述代码

2) 请确保在与UIView的
animateWithDuration:animations:completion:
API一起使用时,使用
scrollToRowAtIndexPath:atScrollPosition:animated:
animated
标记保留为
NO

3) 您还可以根据距离(内容大小、垂直滚动的高度)计算持续时间,即持续时间取决于源索引路径和目标索引路径之间的距离

Please try below code.

[self beginUpdates];

/*
       Insert your sections
*/

[self endUpdates];

[self performSelector:@selector(scroll) withObject:nil afterDelay:0.1];

-(void)scroll 
{
    if([Your array count] > 0)
    {
        [YOURTABLENAME scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    }
}

I Hope above code is work for you.

谢谢

[self-ScrollToRowatineXpath:indexPath atScrollPosition:UITableViewScrollPosition底部动画:否];为什么动画属性为“否”?我尝试了“是”和“否”…我只是在问题中发布了“否”.您可以尝试使用延迟“0.4”滚动,看看它有什么作用。。使“withRowAnimation:UITableViewRowAnimationFade”。@user2384694是否继承了表视图?@user2384694是否使用UITableViewController?
Please try below code.

[self beginUpdates];

/*
       Insert your sections
*/

[self endUpdates];

[self performSelector:@selector(scroll) withObject:nil afterDelay:0.1];

-(void)scroll 
{
    if([Your array count] > 0)
    {
        [YOURTABLENAME scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    }
}

I Hope above code is work for you.