Dragging a tableView to the bottom and then updating the data is a common scenario. Using MJRefresh, or GTMRefresh, is a good way to update. But if you do it yourself, it’s much easier to do it without third party dependencies, and you can learn how third-party libraries work.

In the following example, a tableView with more than one screen of data will trigger the onRefresh function when dragged near the bottom of the tableView (with one value set to 100) :

 import UIKit
 @UIApplicationMain
 class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        self.window = UIWindow(frame: UIScreen.main.bounds)
        letpage = Page() page.view.backgroundColor = .blue self.window! .rootViewController = page self.window? .makeKeyAndVisible()return true
    }
 }
 class Page: UIViewController {
    var a : Table!
    override func viewDidLoad() {
        super.viewDidLoad()
        a  = Table()
        a.frame = UIScreen.main.bounds
        self.view.addSubview(a)
    }
 }

class Table : UITableView,UITableViewDataSource,UITableViewDelegate{
    let lang = "java,swift,js,java,swift,js,java,swift,js,java,swift,js,java,swift,js,java,swift,js,java,swift,js,java,swift,js,java,sw ift,js,java,swift,js,java,swift,js,".components(separatedBy: ",")
    let os = ["Windows"."OS X"."Linux"]
    override init(frame: CGRect, style: UITableViewStyle) {
        super.init(frame:frame,style:style)
        self.dataSource = self
        self.delegate = self
//        lang = split(langStr) {$0= =","} } required init? (coder aDecoder: NSCoder) { super.init(coder:aDecoder) } func numberOfSections(in: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return lang.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let arr = lang
        let a = UITableViewCell(style: .default, reuseIdentifier: nil)
        a.textLabel?.text = String(describing:arr[indexPath.row])
        return a
    }
    letThreshold: CGFloat = 100.0 // threshold from bottom of tableView var isLoadingMore =false // flag
    func onRefresh() {print("new data")
    }
    private func addObserver() {
        self.addObserver(self, forKeyPath: "contentOffset", options: .new, context: nil)
    }

    private func removeAbserver() {
        self.removeObserver(self, forKeyPath:"contentOffset")
    }
    var curentContentHeight : CGFloat = 0
    override open func observeValue(forKeyPath keyPath: String? , of object: Any? , change: [NSKeyValueChangeKey : Any]? , context: UnsafeMutableRawPointer?) {print(contentOffset.y)
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
//        print(scrollView.contentOffset.y,self.contentSize.height,UIScreen.main.bounds.height)

        let contentOffset = scrollView.contentOffset.y
        let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
//        print(maximumOffset,contentOffset) // When we are 100 short of the bottom...if! isLoadingMore && (maximumOffset - contentOffset <= threshold) { self.isLoadingMore =true
            let when = DispatchTime.now() + 2
            DispatchQueue.main.asyncAfter(deadline: when) {
                self.onRefresh()
                self.isLoadingMore = false}}}}Copy the code

When you drag the TableView, the overall content is offset to enhance the operation. And the key to doing this is that the tableView does a delegate to the UIScrollViewDelegate, as soon as it joins

func scrollViewDidScroll(_ scrollView: UIScrollView) Copy the code

Implementation, so when you drag, you call this function, and you let the TableView class get notified, and within this function you can determine when to load new data. Here we limit it to a variable called threshold, which triggers the onRefresh event when the drag is this far from the bottom (that is, the height of the complete content). In this event, you can add the code you need to load the data.

Reference: anasimtiaz.com/?p=254