当UITableView嵌套在UIScrollView中时如何触发scrollsToTop

发布于 2024-12-07 03:30:15 字数 207 浏览 0 评论 0原文

我想知道当 UITableView 嵌套在也观察此函数的 UIScrollView 中时,是否有人有幸从用户点击状态栏触发 UITableView 的 scrollsToTop (或通过任何其他方式)?我知道这可能不是一个好的做法,但在这种情况下,用户体验类型需要这种类型的层次结构。

不管怎样,我已经看到了一大堆提案,从私有方法(显然不会发生)到在状态栏上添加假窗口(也不会发生)。

I was wondering if anyone had had any luck triggering scrollsToTop (or by any other means) for a UITableView from the user tapping on the status bar when the UITableView is nested inside a UIScrollView that also observes this function? I know this probably isn't good practice, but in this instance the UX kind of calls for this type of hierarchy.

Either way, I've seen a whole bunch of proposals ranging from private methods (obviously not going to happen) to adding fake windows over the status bar (also not going to happen).

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

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

发布评论

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

评论(4

滥情哥ㄟ 2024-12-14 03:30:15

好的,所以这里的答案有两个方面:

  1. 在同一个 UIView 上,不能有多个 UIScrollView(或从 UIScrollView 派生或使用 UIScrollView 的类 - 即 UITableView),并且属性 scrollsToTop 设置为 YES。选择您想要拥有该功能的一项,并确保其他所有都没有

    例如,执行以下操作:

    scrollView.scrollsToTop = NO;
    tableView.scrollsToTop = 是; // 或未设置
    
  2. 实现 UIScrollView 委托方法 scrollViewShouldScrollToTop:返回 YES 如果调用 UIScrollView 是 UITableView。

支持 此答案提及非多个 scrollsToTop 选项。

Ok, so the answer here is two fold:

  1. You cannot have more than one UIScrollView (or classes deriving from or using UIScrollView - i.e. UITableView) on the same UIView with the property scrollsToTop set to YES. Pick the one you want to have the feature and make sure all others are no

    For example, do this:

    scrollView.scrollsToTop = NO;
    tableView.scrollsToTop = YES; // or not set
    
  2. Implement the UIScrollView delegate method scrollViewShouldScrollToTop: and return YES if the calling UIScrollView is the UITableView.

Props to this answer for mentioning the non-multiple scrollsToTop option.

噩梦成真你也成魔 2024-12-14 03:30:15

只是想分享我编写的一个有助于调试这些情况的小函数。正如其他人所提到的,您必须确保只有一个滚动视图打开了scrollsToTop。如果您嵌入复杂的视图层次结构,则可能很难找出哪个滚动视图是罪魁祸首。只需在创建视图层次结构后调用此方法,就像在 viewDidAppear 中一样。 level 参数只是为了帮助缩进,你应该用 0 作为种子

-(void)inspectViewAndSubViews:(UIView*) v level:(int)level {

NSMutableString* str = [NSMutableString string];

for (int i = 0; i < level; i++) {
    [str appendString:@"   "];
}

[str appendFormat:@"%@", [v class]];

if ([v isKindOfClass:[UITableView class]]) {
    [str appendString:@" : UITableView "];
}

if ([v isKindOfClass:[UIScrollView class]]) {
    [str appendString:@" : UIScrollView "];

    UIScrollView* scrollView = (UIScrollView*)v;
    if (scrollView.scrollsToTop) {
        [str appendString:@" >>>scrollsToTop<<<<"];
    }
}

NSLog(@"%@", str);

for (UIView* sv in [v subviews]) {
    [self inspectViewAndSubViews:sv level:level+1];
}}

。在视图控制器的主视图上调用它。

在日志中,您应该看到>>scrollsToTop<<<在每个打开它的视图旁边,可以轻松找到错误。

Just wanted to share a little function I wrote that helps debug these situations. As others have mentioned, you have to make sure only ONE scroll view has scrollsToTop turned on. If you embed complex view hierarchies it may be difficult to figure out which scroll view is the culprit. Just call this method after your view hierarchy is created, like in viewDidAppear. The level parameter is just to help indentation and you should seed it off with 0.

-(void)inspectViewAndSubViews:(UIView*) v level:(int)level {

NSMutableString* str = [NSMutableString string];

for (int i = 0; i < level; i++) {
    [str appendString:@"   "];
}

[str appendFormat:@"%@", [v class]];

if ([v isKindOfClass:[UITableView class]]) {
    [str appendString:@" : UITableView "];
}

if ([v isKindOfClass:[UIScrollView class]]) {
    [str appendString:@" : UIScrollView "];

    UIScrollView* scrollView = (UIScrollView*)v;
    if (scrollView.scrollsToTop) {
        [str appendString:@" >>>scrollsToTop<<<<"];
    }
}

NSLog(@"%@", str);

for (UIView* sv in [v subviews]) {
    [self inspectViewAndSubViews:sv level:level+1];
}}

Call it on your view controller's main view.

In the log, you should see >>>scrollsToTop<<< next to every view that has it turned on, making it easy to find the bug.

灯角 2024-12-14 03:30:15

帮助我解决此问题的一件事是:

  • 验证视图层次结构中的所有其他 UIScollView 是否设置为 scollsToTop = NO
  • 使用 添加子视图控制器作为子视图控制器addChildViewController: 仅将视图作为子视图添加到父视图中是不够的。

One thing that helped me fix this problem is:

  • Verify every other UIScollView in the view hierarchy is set to scollsToTop = NO
  • Add sub view controllers as child view controllers using addChildViewController: It is not sufficient to only add the view as a subview to the parent's view.
梨涡 2024-12-14 03:30:15
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

[gridTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];

希望对你有帮助:)

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

[gridTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];

Hoping to help you :)

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