UITableView scrollToRowAtIndexPath 的奇怪行为:

发布于 2024-09-08 17:13:40 字数 569 浏览 1 评论 0原文

我必须将 UITableView 的初始滚动位置设置为第一行以外的行(例如,第二部分的第一行)。

在我的视图控制器的 viewDidLoad 中,我写道:

- (void)viewDidLoad
{
    // self.view is a container view, not UITableView
    [super viewDidLoad];
    [table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]
       atScrollPosition:UITableViewScrollPositionTop animated:NO];
}

当屏幕加载时,滚动位置不会立即设置。否则,当 animated 参数设置为 YES 时,将立即设置位置,而不使用任何动画。 发生了什么事?

设置 UITableView 初始行位置的正确方法是什么?

I have to set an initial scroll position of my UITableView to a row other than the first one (for example, the first row of the second section).

In my view controller's viewDidLoad I write:

- (void)viewDidLoad
{
    // self.view is a container view, not UITableView
    [super viewDidLoad];
    [table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]
       atScrollPosition:UITableViewScrollPositionTop animated:NO];
}

When the screen loads, the scroll position is not set immediately. Otherwise, when the animated parameter is set to YES, the position is set immediately without any animation. What's going on?

What's the correct way to set initial row position for UITableView?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆光飞翔i 2024-09-15 17:13:40

最好的地方应该是ViewDidAppear。此外,您应该调用 cellWillDisplay 中的滚动方法来确定。请记住在调用滚动方法之前检查indexPath

Best place should be ViewDidAppear. Moreover, you should invoke scroll method in cellWillDisplay to make sure. Remember to check indexPath before invoking scroll method

埋葬我深情 2024-09-15 17:13:40

只是瞎猜,但是您是否尝试过将此代码放入 viewDidAppear{} 中?

Just a shot in the dark, but have you tried to put this code into viewDidAppear{}?

兔姬 2024-09-15 17:13:40

这在 viewDidLoadviewWillAppear 中不起作用,因为那时表很可能尚未填充(至少在我的情况下没有)

尝试将其放入一旦你的表加载并调用它,就会使用 NSTimer 来代替。

顺便说一句,我很快将此代码(带有动画)放入导航栏按钮项目中,并且在表格加载完成后效果非常好。

This won't work in viewDidLoad or viewWillAppear since the table most likely haven't been populated by then (at least it wasn't in my case)

Try putting it in an NSTimer instead once your table loads and calling it then.

As an aside, I quickly put this code (with animation) into a nav bar button item and it worked very well after the table was finished loading.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文