在UITableView的底部加载更多 [英] Load More After Coming to Bottom of UITableView

查看:159
本文介绍了在UITableView的底部加载更多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的应用,我使用的是在底部加载更多属性,如下所示。它实际上工作得很好;唯一的问题是,当用户到达buttom时,加载更多功能正在工作,对于用户来说,它看起来像应用程序冻结了一段时间,因为没有动画视图,如 UIRefreshcontrol 。如何在加载新数据之前显示动画。我发现一些 UIBottomrefreshcontrol 属性作为单独的库,但它们都在Objective-c中。

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

    }
}


推荐答案

在UITableViewController中添加 UIActivityIndi​​catorView (即微调器)。将插座连接到代码:

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

@IBOutlet weak var spinner: UIActivityIndicatorView!

将属性添加到 UITableViewController 以保持跟踪您当前正在加载更多数据,这样您就不会尝试两次:

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

启动微调器动画,然后调用 refreshResults2()

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

在后台线程上运行 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
        }
    }
}

这篇关于在UITableView的底部加载更多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆