UITableview scroll whole cell height - ios

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.

Related

Horizontal scroll in collection view

I wish to do something when my collection view cell is scrolled horizontally. But I do not know which function gets called when it is getting scrolled.
I have a collection view cell inside of a table view cell, and the collection view cell scrolls horizontally
If you use storyboard, in Attributed Inspector you will find an option that Scroll Direction, make it Horizontal, default this is set to Vertical
programitically
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
let collectionView = UICollectionView(frame: frame, collectionViewLayout: layout)
There are several methods that will notify you when a collection view gets scrolled, all of them are contained in UIScrollViewDelegate, and thus in UICollectionViewDelegate protocol, which inherits from the former.
scrollViewDidScroll(_:) is indeed called each time the content offset changes. That means not only the active scrolling issued by the user, but also scrolling by inertia, programmatic scrolling and bouncing. You can use this method to react to the scrolling distance, for example, by querying the contentOffset property of the scroll view.
scrollViewWillBeginDragging(_:) is, in contrast, getting called only at the beginning of scrolling issued by the user (that's why in the documentation you can see that this method may be called only after some delay, since the scroll view's gesture recognizer needs time to decide if it's a tap or a pan gesture). It will not be called again until the user lifts their finger and starts scrolling again.
scrollViewWillBeginDecelerating(_:) is called when the user-issued scrolling discussed above ends, but the scroll view will continue scrolling further to achieve this inertia feeling. Again, it will not get called again until the user lifts their finger one more time.
That's basically it. If this still doesn't narrow down the event that you want to track (for example, you want to become notified when the user starts scrolling at the initial position only), you will need to set some flags or track additional properties.
To track horizontal scrolling for instance, you will need to either compare the scroll view's previous content offset to the current or check the scrolling velocity via scrollView.panGestureRecognizer.velocity(in: collectionView).
First, set your collectionview's delegate to your viewcontroller (or view)
Implement UIScrollViewDelegate methods inside your viewcontroller and you can track your collectionview's scrolling.

UITableView scroll at top issue iOS

I have a tableview and I would like to know when user scroll at top and if in those moment the tableview is not at top (for example its at middle) I just implement the delegate method scrollViewDidScroll and check the value of content offset and its ok.
My problem is when the tableview is is already at the top and scrollViewDidScroll is not called (I want to know if the user do the gesture for scroll to the top even if it is already at the top).
For this reason I thought to add a pan gesture to tableview but I saw that create problems about scrolling and the implementation is cumbersome.
I ask to you if you know another way to achieve this or the only way is adding pan gesture.
You should try to use scrollViewWillBeginDragging or scrollViewWillEndDragging

Adding a Scrolling Subview to a UITableView (NOT a Cell)

I'm creating my views programmatically. I have a UITableView in my UIViewController subclass that I want to add a scrolling subview to that is not a cell. I want to add some text-based subview to the UITableView that scrolls with the table and starts out above y=0 so the user will only see it if he pushes the table down. That is, it should reside above the first section of my table. If it helps for visualization, I intend to make something similar to those "scroll down to refresh" features and want some indication to the user that scrolling down causes a refresh. Is there any way to do this without something messy like using another UITableViewCell to represent it or abusing the UITableView delegate methods to move a view around whenever the user scrolls?
Simply using [tableView addSubview:] in my viewWillLoad only makes it appear for a split-second then disappear once the table data is loaded. This seems weird to me because UITableView is a subclass of UIScrollView, which is meant to hold other views in it. Using [tableView.backgroundView addSubview] does nothing.
P.S. Why not use a UIRefreshControl for this? I'm still undecided but leaning towards not using one because I don't like how slow that spinning wheel "feels" when the refreshes are usually very very quick. I've been looking at other options like flashing the background subtly and only showing a wheel if it's taking a longer time than usual.
I You can implement pull to refresh with only a table view
To do this using the scroll view delegate, since tableview is a subclass of scroll view.
Set view controller to be the tableview delegate and implement
(void)scrollViewDidScroll:(UIScrollView *)scrollView
When scrollview content offset y value passed a point, add a label to the viewcontroller.view not tableview. When you scroll back or release, remove the view.
You might also be able to add label to the table view and set the frame origin to negative y value, so when you pull the label will move into view (Never tested this do might not work)

iOS prevent subview of tableview from scrolling with tableview

I have added a subview to my tableview and when ever the user scrolls the tableview, the subview scrolls with it. How do I prevent this? I know it's probably along the lines of not adding the view to the tableview's subviews, but I have no knowledge of any other ways to do this. Thanks.
If you want to make a view a subview of the table view, then you can make it floating (non-scrolling) by changing its origin.y value in the scrollViewDidScroll method.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
self.iv.frame = CGRectMake(self.ivOrigin.x, self.ivOrigin.y + self.tableView.bounds.origin.y, self.iv.frame.size.width, self.iv.frame.size.height);
}
In this example, "iv" is a property for an image view, and "ivOrigin" is a property for the initial origin of the image view (defined when I created the image view and its frame in viewDidLoad).
The UITableView is built and intended to be a view of things that scroll.
So, you can either fight that, which as you're discovering is quite hard since everything about the component is built and focused around scrolling and fast display of a subset of the full list data... Or, you can not fight it and put your static item on top of the table as a fixed-position item.
If there's a reason you can't add the table view and your animate-out item in your main view, you can always add a custom UIView class that contains both the table view and your animated view. Have your custom view class expose the contained table view as a .table property, and the container you're putting things in can be tweaked to use "mycontainerObject.tableview" instead of just "tableview" where needed.
Yes, it's a little more work to write the custom UIView subclass and give it a couple properties to hold the UITableView and whatever UIView you're animating out.. but it's likely a lot safer in the long run than trying to "hack" into the UITableView's methods and view hierarchy to try to give it a "fixed in place" behavior.

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