Issue with row selection UITableView inside UIScrollView - ios

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.

Related

Scrolling issue in nested UITableview in iOS

I'm creating a layout where the data should scroll vertically and each rows data should scroll vertically. To accomplish this I have used UITableView as parent and inside each UITableViewCell, I am adding UITablview for vertical scroll.
The parent UITableView has only two section, each section has child UITableView. The Child tableView has more than 50 rows. I am facing scrolling issue.
I want that the child UITableView scrolls only when the respective section reaches to top of the screen. Now the behaviour is it scrolls independentaly.
How we can achieve this?
You have to make a custom subclass of your inner UITableView and override following method based on your condition and return true/false:
touchesShouldBegin(_:with:in:)
Try by setting following property to the main table view.
self.tableView!.delaysContentTouches = true
self.tableView!.canCancelContentTouches = true
Another option is, Use TableView inside CollectionView

UITableView with a UIView underneath the Cells

I have a UITableView and below the custom cell (in IB) I have inserted a UIView. The UIView gets set to hidden when viewDidLoad() gets called and is only displayed when there is no data in the UITableView. This works great and servers my purpose.
The problem is that even when the UIView is hidden, the scrolling on the UITableView considers the view to be present. i.e. The vertical scroll with scroll well below the last cell, covering the area where the UIView exists - even though its blank and nothing is displayed.
I have tried to hide the UIView but this doesn't help either. How do I get the scroll to not extend to the area covered by the UIView?
Edit:
The image on the left is how IB looks. I have added a UIView as a subview of the TableView. The image on the right is the large scroll space below the last cell.
Expected behavior: When the view is hidden, the scroll view only scrolls until the last cell.
If your view is displayed only when there is no cells, you could just add it over your UITableView (or behind it if your UITableView's background is clear) and hide it when you have cells to display. Just be sure to add your view as a sibling of your UITableView and not as a child view of your UITableView.

UITableView header will not stick to the top of UITableView

I have a UIViewController in my Storyboard and in that I have a UIView which contains a UITableView.
I added another small UIView into the top of the UITableView which contains some buttons and text.
Basically, when the run the app, the UIView which I have placed in the UITableView goes to the bottom of the UITableView for some reason.... It scrolls perfectly with the UITableView, but it stays at the bottom...
How can I get the inserted UIView to stay at the top of my UITableView and still scroll with the UITableView?
Thanks, Dan.
What you're describing is the table view header. You don't add it to the table directly, but rather, you assign it to the table's tableHeaderView property like this:
self.tableView.tableHeaderView = myHeaderView;
Or you can just drag and drop a view into the storyboard at the top of the table view above the first section.

UITableView inside UICollectionView handling touches.

Collection view consists of a single row, of horizontally aligned cells, which size is the same size as the collection view's bounds, single cell fills entire screen.
The problem is that the collection view seems to be intercepting all of the pans. How can I forward them to the table so I can also scroll the table vertically.
I want vertical pan to be delivered to the table inside the cell, so it can scroll up and down. I want horizontal pan to be delivered to the collection view, so it can scroll horizontally.
Any ideas? Thanks.
For UITableView inside CollectionView using storyBoard, please follow these steps:
-Drag CollectionView to UIViewController, drag datasource to UIViewController(don't drag delegate). Add datasource methods inside ViewController.m
-Create Cell:CollectionViewCell class. choose class for Cell in storyBoard to Cell class,specify reuse ID.
-Drag tableView inside collectionCell square. delegate, datasource drag to CollecionCell Square too. Add tableView datasource, delegate inside Cell.m
-Create CellDelegate when implement tableViewDidSelect inside it. Transfer this delegate to UIViewController to perform other action
Sample code: https://github.com/lequysang/github_zip/blob/master/TableViewInCollection.zip
Swift2 update - We don't need to do all the above steps. Just making the controller the delegate and datasource of both the tableview and collection view works just fine.
I've been able to make this work without using storyboards--although, I use XIBs for my views but only to add the Autolayout rules. All customisation (colors, fonts, labels, etc.) are set in code. In any case, this should also work for cells that are being built programatically.
How to do it:
The most important thing to remember is to add your custom view to the contentView property of the cell and not inside the cell itself. If you are using XIBs for the custom UICollectionViewCell, leave it blank. Instantiate your custom view (where the UITableView is), then add it as a subview of the UICollectionViewCell's contentView. If you add the table vie directly inside the collection view cell, the table view will not vertically scroll.
Implement shouldSelectItemAtIndexPath and shouldHighlightItemAtIndexPath of the UICollectionViewDelegate and make both return false, so that tapping on the collection view cell does not intercept with taps intended for the table view inside.
Done with Xcode 7.3 for iOS 9+.

How should I achieve a non-bouncing UITableView when using UITableViewController?

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.

Resources