从setLoadingScreen删除这些行:
self.spinner.startAnimating()
self.myTableView.addSubview(loadingView)
并将它们添加到refreshData:
func refreshData(sender: UIRefreshControl) {
myTableView.addSubview(loadingView) //here
spinner.startAnimating() //also, start the spinner
myTableView.reloadData()
refreshControl.endRefreshing()
}
,并预期它应该工作。 发生了什么事情是在viewDidLoad中,当你调用setLoadingScreen时,你不仅设置了loadingView,而且还将它添加到视图层次结构(从而显示它)。正如Jitendra所说,当你完成它的时候,别忘了删除loadingView。
后编辑: 如果你想要的只是一个正常的refreshControl,没有定制,你可以摆脱大部分代码。所有你需要的是:
声明如下:
var refresher: UIRefreshControl!
在viewDidLoad中:
refresher = UIRefreshControl()
refresher.addTarget(self, action: "refreshData:", forControlEvents: .ValueChanged)
tableView.addSubview(refresher)
在refreshData:
//get the new data
refresher.endRefreshing()
tableView.reloadData()
你并不需要 “loadingView”,“微调器“,”加载标签“,你不需要”setLoadingScreen“和”removeLoadingScreen“功能。 UIRefreshControl将为您完成所有工作。让我知道它是否有效。
后来的后来EDIT(斯威夫特2)
如果你也想以编程方式启动refreshcontrol当你第一次显示表,并有在你获取数据运行它:
func showSpinnerWhileFetchingData() {
refresher.beginRefreshing()
tableView.setContentOffset(CGPointMake(0, tableView.contentOffset.y - refresher.frame.size.height), animated: true)
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {[unowned self] in
//fetch you data here. you are now on a background queue
//to quick test it, uncomment the code below
//for i in 1...2000 {
// self.yourArrayThatIsPopulatingTheTable.append("\(i) hot potatoes")
//}
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.tableView.reloadData()
self.refresher.endRefreshing()
})
})
}
调用此函数在看来会出现。 而摆脱animateTable功能,除非你真的想通过排排动画的效果,在这种情况下:
你要么让它因为它是,它不会是相当明显的(实际上,根本看不到...),但它会减慢你的用户界面
或者,如果你想花时间和你想交易特殊效果的响应,你必须设置一个完成处理器 并在上一次完成后为每一行设置动画动画,这是另一个话题。
我希望它能帮助:)
编辑最后的编辑,翻译成斯威夫特3
func showSpinnerWhileFetchingData() {
refresher.beginRefreshing()
tableView.setContentOffset(CGPoint(x: 0, y: tableView.contentOffset.y - refresher.frame.size.height), animated: true)
let qualityOfServiceClass = DispatchQoS.QoSClass.background
let backgroundQueue = DispatchQueue.global(qos: qualityOfServiceClass)
backgroundQueue.async(execute: {[unowned self] in
//fetch you data here. you are now on a background queue
//to quick test it, uncomment the code below and modify the name of the array to match the array you are using
//for i in 1...2000 {
// self.yourArrayThatIsPopulatingTheTable.append("\(i) hot potatoes")
//}
//sleep(5) //alternately, you can use this to test. don't forget to remove this sleep line after you test
DispatchQueue.main.async(execute: {() -> Void in
self.tableView.reloadData()
self.refresher.endRefreshing()
})
})
}
我应该在哪里把我的新removeLoadingScreen()方法? – user8000557
请参阅编辑答案。 –
我试过了,它仍然不会自行停止,它只会在我下拉刷新表格后停止并消失 – user8000557