2

I have a problem with a UITableView not detecting touches.

In an iPhone-only app, I have a UIViewController, which containts:

  • UIScrollView
    • UIView (let's say a content view)
      • Some labels
      • UITableView (last)

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.

Here comes the problem: when the labels contain only some words and the UITableView does not exceed the iPhone screen size, everything works ok. But when they grow, and the UITableView gets pushed down, when I scroll down I can't click on the cell anymore. 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.

Here is an example:

Clickable:

enter image description here

Unclickable:

enter image description here

Alessandro
  • 1,046
  • 1
  • 11
  • 19

3 Answers3

4

Do the following thing:

yourView.clipToBounds = true

Now, if UITableView does not appears means your UIView is not same bigger to hold down UITableView.

Make sure that your UIView height is bigger to hold the contents in it and then try to tap on it.

Updated:

If you are using AutoLayout, then do the following thing.

  1. Give the fix height to UIView
  2. Take the outlet of height constraint of UIView
  3. Now, in viewDidLayoutSubviews change the constraint of UIView to UITableView contentSize height.

    self.heightConstraint = self.tableView.contentSize.height

Let me know, if this helps!

Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
0

Adding some info to the @Rémy Virin's answer here:

From Apple Documentation, you shouldn't embed a UITableViewinside a UIScrollView.

Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. 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.

Community
  • 1
  • 1
Bista
  • 7,869
  • 3
  • 27
  • 55
  • 1
    So I should take the table outside the content view, and set `self.tableView.tableHeaderView = myContentView`? But isn't the table still inside the scrollview, or should I remove it as well? And, if yes, would the table automatically scroll the header view? Thanks. – Alessandro Sep 07 '16 at 12:53
  • Just drag an drop an UIView just above the table view...the View will automatically become an Header View of the table. there you can place the contents. – Bista Sep 07 '16 at 13:20
0

If you use Autolayout, no need to specify contentSize to UIScrollView as it will automatically expand by taking the height of its subview.

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.

UITableView becomes unresponsive when it extends below its container view.

Ram Gandhi
  • 707
  • 8
  • 24
  • I decided to manually specify `contentSize` because I couldn't make the scrollview work properly with auto layout. Is it possible to set an auto layout constraint to extend the `UIView` based on the `tableview height`? EDIT. Here is the code i use: `contentView.frame.size.height = self.answerTable.frame.origin.y + self.answerTable.frame.size.height scrollView.contentSize.height = self.answerTable.frame.origin.y + self.answerTable.frame.size.height`. Is it right? – Alessandro Sep 07 '16 at 13:00
  • Try this method: init(item:attribute:relatedBy:toItem:attribute:multiplier:constant:) – Ram Gandhi Sep 07 '16 at 13:10
  • For the view in the screenshots you showed, UITableView itself will do the stuff. You can create this by using custom sectionHeaders and tableViewCells – Ram Gandhi Sep 07 '16 at 13:13
  • I don't understand, what should I init with this method? – Alessandro Sep 07 '16 at 14:22