[英]UITableView inside UIScrollView - Cell not clickable
I have a problem with a UITableView not detecting touches. 我有一个问题,UITableView没有检测到触摸。
In an iPhone-only app, I have a UIViewController, which containts: 在一个只有iPhone的应用程序中,我有一个UIViewController,它包含:
The labels have a dynamic height, because they contain some text that I must retrive from the web, so I'm using auto layout. 标签有一个动态高度,因为它们包含一些我必须从网上回溯的文本,因此我使用自动布局。 I'm setting the content size of the UIScrollView inside the
viewDidLayoutSubviews
method, and I'm doing this by summing UITableView.frame.origin.y
and its height. 我在
viewDidLayoutSubviews
方法中设置了UIScrollView的内容大小,我这样做是通过对UITableView.frame.origin.y
及其高度求和来实现的。
Here comes the problem: when the labels contain only some words and the UITableView does not exceed the iPhone screen size, everything works ok. 问题出在这里:当标签只包含一些单词并且UITableView不超过iPhone屏幕尺寸时,一切正常。 But when they grow, and the UITableView gets pushed down, when I scroll down I can't click on the cell anymore.
但是当它们成长,并且UITableView被推下时,当我向下滚动时,我不能再单击该单元格了。 Also, if when loading the view the table is half visible and half not, I can click the visible cells, but if I scroll down, I can't click the others.
此外,如果加载视图时表格是一半可见而一半不可见,我可以单击可见单元格,但如果我向下滚动,则无法单击其他单元格。
I'm using swift 2 and Xcode 7. 我正在使用swift 2和Xcode 7。
Here is an example: 这是一个例子:
Clickable: 可点击:
Unclickable: 不可点击:
Do the following thing: 做以下事情:
yourView.clipToBounds = true
Now, if UITableView
does not appears means your UIView
is not same bigger to hold down UITableView
. 现在,如果
UITableView
没有出现意味着你的UIView
不大于按住UITableView
。
Make sure that your UIView
height is bigger to hold the contents in it and then try to tap on it. 确保你的
UIView
高度更大,以容纳其中的内容,然后尝试点击它。
Updated:
更新:
If you are using AutoLayout
, then do the following thing. 如果您使用的是
AutoLayout
,请执行以下操作。
UIView
UIView
修复高度 UIView
UIView
的高度约束出口为例 Now, in viewDidLayoutSubviews
change the constraint of UIView
to UITableView
contentSize
height. 现在,在
viewDidLayoutSubviews
中将UIView
的约束更改为UITableView
contentSize
高度。
self.heightConstraint = self.tableView.contentSize.height self.heightConstraint = self.tableView.contentSize.height
Let me know, if this helps! 让我知道,如果这有帮助!
Adding some info to the @Rémy Virin's answer here: 在@RémyVirin的答案中添加一些信息:
From Apple Documentation, you shouldn't embed a UITableViewinside a UIScrollView. 从Apple文档中,您不应该将UITableViewinside嵌入UIScrollView。
Important: You should not embed UIWebView or UITableView objects in UIScrollView objects.
重要提示:您不应在UIScrollView对象中嵌入UIWebView或UITableView对象。 If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.
如果这样做,可能会导致意外行为,因为两个对象的触摸事件可能混淆和错误处理。
Addition: Solution Since you want to scroll the content above table also, the one solution is place a Header View
for the Table and place the Content over there to make it Scroll along the Table View. 添加: 解决方案由于您还要滚动表格上方的内容,因此一个解决方案是为表格放置
Header View
并将内容放在那里以使其沿表格视图滚动。
If you use Autolayout
, no need to specify contentSize
to UIScrollView
as it will automatically expand by taking the height of its subview. 如果使用
Autolayout
,则无需将contentSize
指定为UIScrollView
因为它将通过获取其子视图的高度自动扩展。
In your case, increase the height of the Base UIView
(content view as mentioned by you) which holds the tableView
when the tableView height
increases. 在你的情况下,增加Base
UIView
的高度(你提到的内容视图),当tableView height
增加时,它保存tableView
。
UITableView
becomes unresponsive when it extends below its container view. 当
UITableView
扩展到其容器视图下方时,它变得无响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.