In an iPad application I'm developing, I want to show a UITableView that does not scroll and does not bounce (meaning that attempting to scroll the tableview up or down does not show the scroll and snap-back effect). The reason I want to do this is that the UITableView contains a fixed number of cells, and divides the visible space equally among the number of cells (in tableView:heightForRowAtIndexPath:). There won't be any need to scroll. UITableView and UIScrollView are implementation details of which the user does not need to be aware.
UIScrollView has a boolean bounces property which I could set to NO if I could access the UIScrollView instance.
I expect that the UITableViewController's UITableView instance has a superview of UIScrollView. However, within my UITableViewController subclass, self.view.superview and self.tableView.superview are both nil. I have determined this within my UITableViewController's -initWithStyle:, -viewDidLoad, and -viewWillAppear: methods.
Is there another way to access the UIScrollView that contains my UITableView? I'm imagining that this technique would involve the least code, although there is no direct accessor to the UIScrollView instance from UITableViewController or UITableView.
Or should I subclass UIViewController instead of UITableViewController, and instantiate a UITableView that is not enclosed in a UIScrollView? Or is there another option I've overlooked?
UITableView is a subclass of UIScrollView, so UITableView has bounces properties (all properties that UIScrollView has).
So to cancel bounces you need to write self.tableView.bounces = NO;. To cancel scroll, self.tableView.scrollEnabled = NO;
And of course you can set this properties using Interface Builder.
Related
Is it possible to let UITableview move up/down whole cell height just like date picker (not stop between the cell top and bottom)?
The class UITableView is a subclass of UIScrollView.
This means that, if the object you specified as your table view's delegate also adopts the UIScrollViewDelegate protocol, that protocol's methods will be called on it whenever the relevant scroll events happen on the table view.
You can use that timing to "fix" the table view's scroll offset in just the right way to simulate the snap-to-row-bnoundary behaviour you're after.
Some examples:
scrollViewDidEndDragging(_:willDecelerate:): Tells the delegate when dragging ended in the scroll view.
scrollViewDidEndDecelerating(_:): Tells the delegate that the scroll view has ended decelerating the scrolling movement.
Which methods to implement, and what exactly to do in their implementations, will depend on what effect you want to achieve. Play around and see.
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:
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.
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.
Give the fix height to UIView
Take the outlet of height constraint of UIView
Now, in viewDidLayoutSubviews change the constraint of UIView to UITableView 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:
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.
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.
I have a UITableView in my view controller, and inside the cell there's a horizontal UIScrollView and inside this scroll view, I created many instances of a custom UIView (loaded from NIB file)
UITableViewCell
-- UIScrollView
-- MyView: UIView
Now MyView is not detecting touches, userInteractionEnabled is set to YES on every view in the hierarchy and I tried both ways
Implement touchesEnded: in MyView
Add a UITapGestureRecognizer to it
Both ways don't work, I guess it's something related to the fact that I have a UIScrollView inside the UITableViewCell
I am writing the application in Swift, not Objective-C, I don't think it matters since I guess it's a UIKit issue but who knows.
If you have any hint, I am all ears.
Thank you
Alternative for this case is add UITableViewCustomCell and add UICollectionView with horizontal scroll in Custom cell . It is more optimized way than adding UIView in scroll view because you can greatly speed things up. Instead of instantiating a lot of cells, you just instantiate as many as needed, i.e. as many that are visible (this is handled automatically). If scrolling to an area in the list where there are "cells" that haven't got their visual representation yet, instead of instantiating new ones, you reuse already existing ones.
Take this about page for Things:
I'm having trouble creating something similar. I just want a UITableView under a UIView with a UIImageView and a UILabel in it.
If I use a UIViewController and so I can position the UITableView downward, I get this error: "Static table views are only valid when embedded in UITableViewController instances."
If I use a UITableViewController with a grouped style and use contentInset on self.tableView to move it down ([self.tableView setContentInset:UIEdgeInsetsMake(150,0,0,0)];) I can't figure out how to place a view above it. If I try to attach anything to self.view it crashes (obviously). Same happens if I attach anything to self.tableView.
I then tried making the UIView the header of my UITableView section (I only need one section) but I can't get it to move up enough. It just sits inside the UITableView almost.
How do I have a UITableView (grouped style) exist with a UIView above it?
This can be achieved easily using the tableHeaderView property of UITableView. If you are using Interface Builder (which it looks like you are), then you can just drag a UIView above the table view and it will be set as the table's header view. All you need is a UITableViewController; no need for UIViewController and manually laying it out.
That's because the view probably isn't placed on top of the table but rather within the table's section 0 header. Or, even more likely, the view in question is just a regular UITableViewCell with a 0 alpha background.
Either of these options would allow the top view to be scrolled out of frame as the user scrolls under every condition.
I recommend [MDAboutController] (https://github.com/mochidev/MDAboutController)
It's easy to integrate and you don't have to waste any time configuring the UITableView.
I have a view controller with following layout:
Container View
UIScrollView
UITableView as a sub view of a UIScrollView
I have another UITableViewController in which I have a few rows and some methods when the row gets selected. Now I want to display this UITableview inside the UIScrollView. So I add the UITableView as a subview of UIScrollView. The table is displayed in the scroll view just fine, but when I tap in the scroll view to select the table's row, then row is being highlighted but the method is not getting called when the row is selected..
PBDashboardPickupTable *dashtable = [[PBDashboardPickupTable alloc]initWithNibName:#"PBDashboardPickupTable" bundle:nil];
[self.scrollView addSubview:dashtable.tableView];
Also I have set scroll view's delayContentTouches to YES and cancelContentTouch to NO from Interface Builder. Also userInteractionEnabled is set to YES... then why is the method not getting called when I tap the table view's row?
Apple specifically warns in the documentation to avoid putting a UITableView inside of 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.
Since UITableView inherits from UIScrollView, I would suggest you add any additional views you need as a tableHeaderView, tableFooterView, or as custom cells in the table.