Load More After Coming to Bottom of UITableView

16,380

Solution 1

Add a UIActivityIndicatorView (i.e. spinner) to your UITableViewController. Connect the outlet to the code:

@IBOutlet weak var spinner: UIActivityIndicatorView!

Add a property to your UITableViewController to keep track that you're currently loading more data so that you don't try to do it twice:

var loadingData = false

Start the spinner animating and then call refreshResults2():

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if !loadingData && indexPath.row == refreshPage - 1 {
        spinner.startAnimating()
        loadingData = true
        refreshResults2()
    }
}

Have refreshResults2() run on a background thread. This will allow your table to still move freely. The animated spinner will tell the user that more data is coming. Once your query returns, update the table data on the main thread.

func refreshResults2() {
    dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)) {
        // this runs on the background queue
        // here the query starts to add new 40 rows of data to arrays

        dispatch_async(dispatch_get_main_queue()) {
            // this runs on the main queue
            self.refreshPage += 40
            self.tableView.reloadData()
            self.spinner.stopAnimating()
            self.loadingData = false
        }
    }
}

Solution 2

  • Swift 3 , Xcode 8

  • func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let lastElement = dataSource.count - 1
        if !loadingData && indexPath.row == lastElement {
            spinner.startAnimating()
            loadingData = true
            loadMoreData()
        }
    }
    

    as per @vacawama async call in swift 3:-

    func loadMoreData() {
        DispatchQueue.global(qos: .background).async {
            // this runs on the background queue
            // here the query starts to add new 10 rows of data to arrays
            DispatchQueue.main.async {
                 // this runs on the main queue
                    self.spinner.stopAnimating()
                    self.loadingData = false
    
            }
        }
    }
    
    Share:
    16,380
    saner
    Author by

    saner

    Updated on August 04, 2022

    Comments

    • saner
      saner almost 2 years

      For my app I am using a "load more at bottom" property as below. It works fine actually; the only problem is that when a user reaches the buttom, while the load more function is working, to the user it seemes like the app is freezing for a while as there is no animation view like in the UIRefreshcontrol. How can I make the animation showing until the new data is loaded. I found some UIBottomrefreshcontrol properties as separate libraries, but they were all in Objective-c.

      override func viewDidLoad() {
          super.viewDidLoad()
      
         refreshResults()
      
       }
      
       func refreshResults() {
      
      
          refreshPage = 40
      
          // here the query starts
      }
      
      
      func refreshResults2() {
      
           // here the query starts to add new 40 rows of data to arrays
      
      }
      
      
      func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,     forRowAtIndexPath indexPath: NSIndexPath) {
      
          if indexPath.row == refreshPage-1 {
              refreshPage = refreshPage + 40
      
              refreshResults2()
      
          }
      }