简体   繁体   English

UITableView 不响应滚动和触摸

[英]UITableView does not respond to scrolling and touches

I've created a custom UIView, and inside that UIView, I add a UITableView like thus:我创建了一个自定义 UIView,在该 UIView 中,我添加了一个 UITableView,如下所示:

    treeView = [[UITableView alloc] initWithFrame:CGRectMake(0, 80, slider.frame.size.width, slider.frame.size.height - 80)];
    [treeView setDelegate:self];
    [treeView setDataSource:self];
    [self addSubview:treeView];
    [treeView release];

When the app loads, the table seems to load fine.当应用程序加载时,表格似乎加载正常。 However, when I try to scroll the table, it does not respond to the touches at all.但是,当我尝试滚动表格时,它根本不响应触摸。

I also implement these delegate/data source methods:我还实现了这些委托/数据源方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

Does anyone have any idea why this is so?有谁知道为什么会这样?

Thanks谢谢

There are several possibilities:有几种可能:

  • Your custom view, or one of its superviews, has userInteractionEnabled set to NO.您的自定义视图或其超级视图之一将userInteractionEnabled设置为 NO。
  • There is another view on top of your custom view, or on top of the table view within the custom view.在您的自定义视图之上或在自定义视图中的表格视图之上还有另一个视图。
  • The custom view that the table view is inside is smaller than the table view (with clipsToBounds=NO, a subview that extends beyond the bounds of its parent can be seen but not normally interacted with). table view 所在的自定义 view 比 table view 小(clipsToBounds=NO,可以看到超出其父级边界的子视图,但不能正常交互)。 Or the same for another view in the stack.或者堆栈中的另一个视图也是如此。
  • The custom view (or one of its superviews) overrides pointInside:withEvent: or hitTest:withEvent: incorrectly.自定义视图(或其超级视图之一)错误地覆盖了pointInside:withEvent:hitTest:withEvent:

Here's another reason why it can happen...这是它可能发生的另一个原因......

The UITableView is a child of another view where the frame is smaller than the table view. UITableView 是另一个视图的子视图,其中框架小于表视图。 It doesn't seem make much sense but it can happen!这似乎没有多大意义,但它可能发生! I had a case ware the parent of the tableview had a width=0.我有一个案例,tableview 的父级有一个宽度 = 0。

Which of course caused pointInside:withEvent: or hitTest:withEvent: to return NO!这当然导致 pointInside:withEvent: 或 hitTest:withEvent: 返回 NO!

For me, The reason why my TabView was not responding is because of the transform animation.对我来说,我的 TabView 没有响应的原因是因为转换动画。 I was animating my Table view using view.layer.transform = CATransform3DMakeScale(0.5,0.5,0);我正在使用view.layer.transform = CATransform3DMakeScale(0.5,0.5,0);为我的 Table 视图设置动画view.layer.transform = CATransform3DMakeScale(0.5,0.5,0); which is 3D transformation property.这是 3D 变换属性。 I found out that by using 2D transformation instead toVC.view.layer.affineTransform = CGAffineTransformMakeScale(0.5, 0.5);我发现通过使用 2D 转换而不是toVC.view.layer.affineTransform = CGAffineTransformMakeScale(0.5, 0.5); Then my table view becomes responding to my touches and scrolling.然后我的表格视图会响应我的触摸和滚动。 I am not sure exactly what is the root cause but this is my observation.我不确定根本原因是什么,但这是我的观察。

Make sure userInteractionEnabled is YES on the view(s) that need to receive touch events.确保需要接收触摸事件的视图上的 userInteractionEnabled 为 YES。 Also try calling becomeFirstResponder on the view as well and call resignFirstResponder when you're done.还尝试在视图上调用 becomeFirstResponder 并在完成后调用 resignFirstResponder。 Sorry I don't have more detail but it's been a while since I did this.对不起,我没有更多细节,但我已经有一段时间没有这样做了。 Good luck!祝你好运!

Maybe someOne will need this answer:也许有人会需要这个答案:

make your view add to cell directly, not to cell.content让您的视图直接添加到单元格,而不是添加到cell.content

This work for me, when not use cell in table, but normal view.这对我有用,当不使用表格中的单元格,而是使用普通视图时。 It seems touches will not forward to contentView in this moment.似乎此时触摸不会转发到 contentView 。

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

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