Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

    }
}
like image 796
saner Avatar asked Sep 06 '15 16:09

saner


People also ask

How do I get top of Tableview?

To scroll to the top of our tableview we need to create a new IndexPath . This index path has two arguments, row and section . All we want to do is scroll to the top of the table view, therefore we pass 0 for the row argument and 0 for the section argument. UITableView has the scrollToRow method built in.

How do I populate UITableView?

There are two main base ways to populate a tableview. The more popular is through Interface Building, using a prototype cell UI object. The other is strictly through code when you don't need a prototype cell from Interface Builder.

Is UITableView scrollable?

Scrolling to a certain point in a table view is a common requirement. For example, you might scroll to show new entries after loading new data or to show that a row has updated data. Since UITableView inherits from UIScrollView it's easy to scroll programmatically.

What is Section in UITableView?

UITableView with sections allows us to separate the list into different categories, so the list looks more organized and readable. We can customize sections as per our need, but in this tutorial, we are covering the basic UITableview with sections. Here's is the video if you prefer video over text. Let Create An App.


1 Answers

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
        }
    }
}
like image 105
vacawama Avatar answered Nov 30 '22 06:11

vacawama