Scrolling Delegate in Tableview

UITableView delegate action when tableView is scrolled?

UITableView is a subclass of UIScrollView and table's delegate can also act as a scroll view's delegate. So you can use all methods from UIScrollViewDelegate for your table (implementing them in table's delegate), e.g.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

(scrollView parameter in this case will be pointer to your tableview)

Scrolling Delegate in TableView

You must use UIScrollViewDelegate in your tableView to intercept scrollView actions with:

class YourClass: YourType, UIScrollViewDelegate {}

Check the official apple documentation

You can handle scrollview looking for scrollViewDidScroll(_:) method.

This is just an example to add more network data when the user scroll to the end, you can use it to trigger your header animation..

let threshold = 100.0 // threshold from bottom of tableView
var isLoadingMore = false // flag

func scrollViewDidScroll(scrollView: UIScrollView) {
let contentOffset = scrollView.contentOffset.y
let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;

if !isLoadingMore && (maximumOffset - contentOffset <= threshold) {
// Get more data - API call
self.isLoadingMore = true

// Update UI
dispatch_async(dispatch_get_main_queue()) {
tableView.reloadData()
self.isLoadingMore = false
}
}
}

UPDATE:

By analizying your animation, it's not simple but not impossible :)
I can see two different states:

Sample Image

And the second state:

Sample Image

You can organize your controller by choose a UIViewController type.
This UIViewController must be composed by:

  • -UINavigationController (as you can see on the top of the images , choose if you want to embedded it or link a navigation controller and set your viewController as the navigation
    rootViewController)
  • -UIPageView (you can use in your main viewController with the UIPageViewControllerDataSource and UIPageViewControllerDelegate, pay attention to the dimension , it cover the 30% of the top of your controller)
  • -UITableView (this is the last layout part, everytime page scroller the datasource can be changed and refreshed to the table)

P.S. The tableViewHeader can be the gray label with the date: Thursday 21 January 2016, as you can see the dimension dont change during animation.

Delegate methods in UIScrollViewDelegate don't respond to table view scrolling event

After a lot of search it might be a Swift 5 compiler problem (I found a similar bug reported there):

The solution is to add @objc to each method:

@objc func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

if scrollView == self.tableView {

let currentOffset: Float = Float(scrollView.contentOffset.y)

if currentOffset < 25 {
//refresh content
}

let offsetY = tableView.contentOffset.y
let contentHeight = tableView.contentSize.height

if offsetY > contentHeight - scrollView.frame.size.height + 25 {
// load more
}

}

}

How to set scrollView delegate from a UITableView?

A table view delegate is a scroll view delegate, so you have already set it.

@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>

iOS tableview how can I check if it is scrolling up or down

Like @maddy said in the comment of your question, you can check if your UITableView is scrolling by using the UIScrollViewDelegate and further more you could check which direction it scrolls to by using both scrollViewDidScroll and scrollViewWillBeginDragging functions

// we set a variable to hold the contentOffSet before scroll view scrolls
var lastContentOffset: CGFloat = 0

// this delegate is called when the scrollView (i.e your UITableView) will start scrolling
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
self.lastContentOffset = scrollView.contentOffset.y
}

// while scrolling this delegate is being called so you may now check which direction your scrollView is being scrolled to
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if self.lastContentOffset < scrollView.contentOffset.y {
// did move up
} else if self.lastContentOffset > scrollView.contentOffset.y {
// did move down
} else {
// didn't move
}
}

Furthermore: You don't need to subclass your UIViewController with UIScrollViewDelegate if you've already subclassed your UIViewController with UITableViewDelegate because UITableViewDelegate is already a subclass of UIScrollViewDelegate



Related Topics



Leave a reply



Submit