iosswiftuitableviewuiscrollviewdelegate

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


I'm trying to do pull to refresh and infinite scrolling to table view without the need for external libraries

I was setting the delegate correctly:

tableView.delegate   = self

But when scrolling inside tableview, scroll methods do not respond to scrolling action for example:

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
        }
        
    }

}

This case happened when I changed the scheme build configuration to "Release", but when getting back to "Debug" it's working correctly.

enter image description here

My deployment target is: 11.0, and XCode Version 10.2 (10E125)


Solution

  • 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
            }
    
        }
    
    }