saner saner -4 years ago 78
Swift Question

Load More After Coming to Bottom of UITableView

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()

}
}

Answer Source

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
        }
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download