I have a scrollview in a ViewController. The scrollview has a UIScrollViewDelegate.On upon that i am adding a Tableview like below:
self.view.addSubview(self.tableView)
But when i am scrolling on tableview cells somehow the scrollview delegate is getting event, which is unexpected.
What can i do to prevent that?
The UITableView is the subclass of UIScrollView and that is the reason you are getting calls to the UIScrollViewDelegate when you are scrolling the cells.
You can not prevent this if you want to have the scrolling enabled for the TableView. I would suggest you check this condition inside your delegate method, for example:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard scrollView != self.tableView else {return}
}
That's normal because UITableView is a subclass of UIScrollView. Thus you should check if scrollView != self.tableView then do what you want to do inside the scrollView delegate method.
Related
Below view is a tableViewController which is implemented and set the delegate and datasource methods and it works perfectly fine but I have no idea how to implement the purple view behavior on scrolling up and down.
How can I implement it?
The below code is the way I detect that view scrolled up or down. but still, no clue to implement this behavior.
override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
let translation = scrollView.panGestureRecognizer.translation(in: scrollView.superview)
if translation.y > 0 {
presenter?.viewScrolledDown()
} else {
presenter?.viewScrolledUp()
}
}
this question covers how to animate a backgroundColor. If the purple view is a UIView, this won't be a problem. It starts behind the tableView and then moves to the front of the tableView. You can handle this by using these functions:
view.bringSubviewToFront(purpleView)
view.sendSubviewToBack(tableView)
You will also need to animate the autoLayout constraints to move the bottom of the purple view up to the top of the tableView.
In my app I have two table views. Both are same height and width and same cell count.
I want when tableview A Scroll Simultaneously tableview B also scroll.
You can use scrollview delegate scrollViewDidScroll for this purpose. This will do the exact thing you want.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == self.tableA{
self.tableB.setContentOffset(self.tableA.contentOffset, animated: false)
}
else if scrollView == self.tableB{
self.tableA.setContentOffset(self.tableA.contentOffset, animated: false)
}
print("scrollViewDidScroll") // to check whether this function is called while scrolling
}
TRY THIS:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
<your tableView Name>.contentOffset = scrollview.contentOffset
}
As Tableview inherits from ScrollView. Try this.
FOR SWIFT 4:
func scrollViewDidScroll(_ scrollView: UIScrollView)
{
<your tableView Name>.contentOffset = scrollview.contentOffset
}
Instead of using tableview you can use UIPickerView with two components.
If you need code let me know.
I have a UICollectionView with 3 full-screen cells and want to know when a cell is focused full-screen upon swipe. The cellForItemAtIndexPath.isFocused method isn't working for me. Is this the correct use of isFocused method or should I do it another way?
What ever, I can get from your question. I think you want to know on which page it is. So, Here is how i did it:
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
for cell in colViewSol.visibleCells {
let indexPath = colViewSol.indexPath(for: cell)
// here add the code whatever you want to do
}
}
I have a UITableView added to UIViewController. Behind that TableView there is UIImageView like this:
The default behavior of a UITableView is to show whitespace when scrolling up and there is no more content to show. I would like to override this and switch to custom UIPanGestureRecognizer that will move the whole tableview down.
To clarify: When the tableview reaches it top I would like to move the entire tableview instead.
I have set up my PanGestureRecognizer and added it to the tableview and tried this:
func scrollViewDidScroll(scrollView: UIScrollView) {
if self.tableView.contentOffset.y == 0{ //tableview reaches its top
panGestureRecognizer.enabled = true
}else{
panGestureRecognizer.enabled = false
}
}
This does not work at all since the the scrollViewDidScroll triggers at the end of the gesture.
Any other ideas on how I can implement this?
I have in my View a long UIScrollView (about 1000 px in height), and at the end of this UIScrollView I have a UITableView.
The cellForRowAtIndexPath is never called (surely i checked the delegate and datasource if they are connected right, and the IBOutlet of the table is strong) but numberOfRowsInSection is getting called.
I tried reloading the TableView when the UIScrollView scrolls so when the table is at focus the cellForRowAtIndexPath might get called, but with no luck.
Did anyone encounter a similar behaviour when trying to use tableview and scrollview together?
Your hierarchy is like this:
A parentView is there. Inside the parent view there is a scroll view and there is a table view. So, your tableview is somewhere at 1000 from origin of parentview.
So, tableview will never become visible to your parentview and no delegates will be fired.
Include your view as a headerView of your UITableView like that:
Update for 2020, swift 5.X. To create a custom UITableView that allows your tableview inside a scrollview!
1) Create a subclass of UITableView:
import UIKit
final class ContentSizedTableView: UITableView {
override var contentSize:CGSize {
didSet {
invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
layoutIfNeeded()
return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
}
}
2) Add a UITableView to your layout and set constraints on all sides. Set the class of it to ContentSizedTableView.
3) You should see some errors, because Storyboard doesn't take our subclass' intrinsicContentSize into account. At runtime it will use the override in our ContentSizedTableView class