ios 在 UIScrollView 内部时 UITableView 滚动问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17974154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:34:27  来源:igfitidea点击:

UITableView scrolling problems when inside a UIScrollView

iphoneiosuitableviewuiscrollviewscroll

提问by Sagito

I have a UIScrollView (with paging) to which I add three UIViews. Each of these UIViews has a UITableView inside. So, the user should be able to scroll horizontally to the page he wants and then scroll vertically in the corresponding table.

我有一个 UIScrollView(带分页),我向其中添加了三个 UIView。这些 UIView 中的每一个都有一个 UITableView。因此,用户应该能够水平滚动到他想要的页面,然后在相应的表格中垂直滚动。

However, some of the tables don't receive the scrolling gestures. Usually the first one does behave good, but the other ones do not. I can't select cells nor scroll the table up or down.

但是,某些表格不会接收滚动手势。通常第一个表现良好,但其他表现不佳。我无法选择单元格,也无法向上或向下滚动表格。

I used the default settings for the UIScrollView, except for these ones defined in the viewDidLoad:

我使用了 UIScrollView 的默认设置,除了在 viewDidLoad 中定义的这些设置:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Load the view controllers
    [self loadViewControllers];

    //Configure the scroll view
    self.scrollView.pagingEnabled = YES;
    self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.scrollView.frame) * viewControllers.count, CGRectGetHeight(self.scrollView.frame));
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.scrollsToTop = NO;
    self.scrollView.delegate = self;

    //Configure the page control
    self.pageControl.numberOfPages = viewControllers.count;
    self.pageControl.currentPage = 0;
}

I can't figure out why I can't scroll some of the tables... Any help would be greatly appreciated.

我不明白为什么我不能滚动某些表格......任何帮助将不胜感激。

Thanks in advance!

提前致谢!

采纳答案by hgwhittle

Things I would check:

我要检查的事情:

  1. Check your View Hierarchies - Is something being laid on top of your UITableView, causing it not to receive a tap?
  2. Are your UITableViews being disabled anywhere? I would set a breakpoint in tableView:didSelectRowAtIndexPath:and see if that method is being called.
  3. Check this post
  1. 检查您的视图层次结构 - 是否有东西放置在您的 UITableView 之上,导致它没有收到点击?
  2. 你的 UITableViews 是否在任何地方被禁用?我会在其中设置一个断点tableView:didSelectRowAtIndexPath:,看看是否正在调用该方法。
  3. 检查这个帖子

I guess those aren't sure-fire answers but hopefully they'll help discover the problem!

我想这些不是肯定的答案,但希望它们能帮助发现问题!

回答by Alberto Vitale

Try to set

尝试设置

self.scrollView.delaysContentTouches = YES;
self.scrollView.canCancelContentTouches = NO;

Maybe the UIScrollView don't pass touch informations to the subviews.

也许 UIScrollView 不会将触摸信息传递给子视图。

回答by nzs

I tried to reproduce a simplified version of your needs using basically Interface Builder and it seems to me it's working using basic coding and using default settings. Can you pls check my quick n dirty Github repo and kindly ask to reply whether it is applicable to your situation or what is missing.

我尝试使用基本的 Interface Builder 来重现您的需求的简化版本,在我看来,它使用基本编码和默认设置工作。您能否检查我的快速和肮脏的 Github 存储库,并请回复它是否适用于您的情况或缺少什么。

https://github.com/codedad/SO_ScrollView_with_Tables

https://github.com/codedad/SO_ScrollView_with_Tables

By default Interface Builder creates UIScrollView and UITableViews enabling:

默认情况下,Interface Builder 创建 UIScrollView 和 UITableViews 启用:

  • Delays Content Touches ON
  • Cancellable Content Touches ON
  • 延迟内容接触
  • 可取消的内容触及

回答by Ben Ferraro

This worked for me

这对我有用

I programmatically added the tableView to my scroll view using addSubview:

我使用编程方式将 tableView 添加到我的滚动视图 addSubview:

UIGestureRecognizerDelegateis needed.

UIGestureRecognizerDelegate需要。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    if ([touch.view isDescendantOfView:self.signUpJammerList]) {

        return NO;
    }
    return YES;
}