简体   繁体   English

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

[英]iPhone SDK - How to scroll a UITableView programmatically with animation?

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. 谢谢你的帮助。

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;
   ....
}

Try commenting out the reloadData call. 尝试注释掉reloadData调用。 If I'm understanding your question correctly, and you're in viewDidLoad , you should not need to call it anyway. 如果我正确理解你的问题,并且你在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和动画开始之间插入一个延迟(我发现四分之一秒工作正常)。

I have two suggestions for you to investigate: 我有两个建议供您调查:

  1. Are you running your app on the simulator or the iPhone itself? 您是在模拟器或iPhone上运行您的应用程序吗? I have noticed some animations behave differently in the simulator. 我注意到一些动画在模拟器中表现不同。

  2. Is it possible you are calling scrollToRowAtIndexPath:atScrollPosition:animated twice in quick succession? 您是否可以调用scrollToRowAtIndexPath:atScrollPosition:快速连续两次动画? This can 'confuse' the animation. 这可能会“混淆”动画。 You should call this function only once while processing a given event. 在处理给定事件时,您应该只调用此函数一次。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM