对于我的应用程序,我使用“在底部加载更多”属性,如下所示。实际上效果很好;唯一的问题是,当用户到达按钮时,虽然加载更多功能正在工作,但对于用户来说,应用程序似乎冻结了一段时间,因为没有像UIRefreshcontrol
。如何使动画显示直到加载新数据。我找到了一些UIBottomrefreshcontrol
属性作为单独的库,但它们都在 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()
}
}
Add a UIActivityIndicatorView
(即微调器)到您的 UITableViewController。将插座连接到代码:
@IBOutlet weak var spinner: UIActivityIndicatorView!
将属性添加到您的UITableViewController
跟踪您当前正在加载更多数据,这样您就不会尝试执行两次:
var loadingData = false
启动旋转器动画,然后调用refreshResults2()
:
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if !loadingData && indexPath.row == refreshPage - 1 {
spinner.startAnimating()
loadingData = true
refreshResults2()
}
}
Have refreshResults2()
在后台线程上运行。这将使您的桌子仍然可以自由移动。动画旋转器将告诉用户更多数据即将到来。查询返回后,更新主线程上的表数据。
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
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)