iPhone SDK - 如何使用动画以编程方式滚动UITableView?

时间:2023-01-05 18:54:14

How would I scroll a UITableView to a specific position with an animation?

如何使用动画将UITableView滚动到特定位置?

Currently I'm using this code to jump to a position:

目前我正在使用此代码跳转到某个位置:

 //tableController -> viewDidLoad
 [self.tableView reloadData];
 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
 [self.tableView scrollToRowAtIndexPath:indexPath
                       atScrollPosition:UITableViewScrollPositionTop
                               animated:YES];

The problem with this code is, that the table jumps right to the right position without any animation. Is there any way to enable the animation or set the duration?

这段代码的问题是,表格没有任何动画就会直接跳到正确的位置。有没有办法启用动画或设置持续时间?

Thanks for any help.

谢谢你的帮助。

4 个解决方案

#1


23  

It works fine for me:

这对我来说可以:

-(void) viewDidAppear:(BOOL)animated{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:n inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath
                    atScrollPosition:UITableViewScrollPositionTop
                            animated:YES];

}

To be sure that this is called only once, you can make a verification like this:

为了确保只调用一次,您可以进行如下验证:

if(!isInitialized){
   isInitialized = YES;
   ....
}

#2


5  

Try commenting out the reloadData call. If I'm understanding your question correctly, and you're in viewDidLoad, you should not need to call it anyway.

尝试注释掉reloadData调用。如果我正确理解你的问题,并且你在viewDidLoad中,你无论如何都不需要调用它。

Also, if you've just gotten done pushing the view controller, you won't get an animated scroll. You'll have to insert a delay (I've found a quarter second works well) between the time that viewDidLoad was called and when your animation starts.

此外,如果您刚刚完成推动视图控制器,您将无法获得动画滚动。你必须在调用viewDidLoad和动画开始之间插入延迟(我发现四分之一秒工作正常)。

#3


0  

I have two suggestions for you to investigate:

我有两个建议供您调查:

  1. Are you running your app on the simulator or the iPhone itself? I have noticed some animations behave differently in the simulator.

    您是在模拟器或iPhone上运行您的应用程序吗?我注意到一些动画在模拟器中表现不同。

  2. Is it possible you are calling scrollToRowAtIndexPath:atScrollPosition:animated twice in quick succession? This can 'confuse' the animation. You should call this function only once while processing a given event.

    您是否可以调用scrollToRowAtIndexPath:atScrollPosition:快速连续两次动画?这可能会“混淆”动画。在处理给定事件时,您应该只调用此函数一次。

#4


0  

I know it's maybe too late, but maybe it is help to others, now you can scroll a table just with [tableView scrollsToTop].

我知道这可能为时已晚,但也许对其他人有帮助,现在你可以用[tableView scrollsToTop]滚动一个表。

#1


23  

It works fine for me:

这对我来说可以:

-(void) viewDidAppear:(BOOL)animated{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:n inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath
                    atScrollPosition:UITableViewScrollPositionTop
                            animated:YES];

}

To be sure that this is called only once, you can make a verification like this:

为了确保只调用一次,您可以进行如下验证:

if(!isInitialized){
   isInitialized = YES;
   ....
}

#2


5  

Try commenting out the reloadData call. If I'm understanding your question correctly, and you're in viewDidLoad, you should not need to call it anyway.

尝试注释掉reloadData调用。如果我正确理解你的问题,并且你在viewDidLoad中,你无论如何都不需要调用它。

Also, if you've just gotten done pushing the view controller, you won't get an animated scroll. You'll have to insert a delay (I've found a quarter second works well) between the time that viewDidLoad was called and when your animation starts.

此外,如果您刚刚完成推动视图控制器,您将无法获得动画滚动。你必须在调用viewDidLoad和动画开始之间插入延迟(我发现四分之一秒工作正常)。

#3


0  

I have two suggestions for you to investigate:

我有两个建议供您调查:

  1. Are you running your app on the simulator or the iPhone itself? I have noticed some animations behave differently in the simulator.

    您是在模拟器或iPhone上运行您的应用程序吗?我注意到一些动画在模拟器中表现不同。

  2. Is it possible you are calling scrollToRowAtIndexPath:atScrollPosition:animated twice in quick succession? This can 'confuse' the animation. You should call this function only once while processing a given event.

    您是否可以调用scrollToRowAtIndexPath:atScrollPosition:快速连续两次动画?这可能会“混淆”动画。在处理给定事件时,您应该只调用此函数一次。

#4


0  

I know it's maybe too late, but maybe it is help to others, now you can scroll a table just with [tableView scrollsToTop].

我知道这可能为时已晚,但也许对其他人有帮助,现在你可以用[tableView scrollsToTop]滚动一个表。