我在尝试停止 Activity 指示器时遇到困难,应将“self.removeLoadingScreen()”放在哪里?在表视图加载数据后,它会不断旋转,我认为将removeLoadingScreen()放在animateTable函数下是理想的选择,但这是不正确的。
var refresher: UIRefreshControl!
func showSpinnerWhileFetchingData() {
refresher.beginRefreshing()
myTableView.setContentOffset(CGPoint(0, myTableView.contentOffset.y - refresher.frame.size.height), animated: true)
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = DispatchQueue.global(Int(qualityOfServiceClass.rawValue), 0)
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
//for i in 1...2000 {
// self.yourArrayThatIsPopulatingTheTable.append("\(i) hot potatoes")
//}
DispatchQueue.main.async(execute: { () -> Void in
self.myTableView.reloadData()
self.refresher.endRefreshing()
})
})
}
func refreshData(sender: UIRefreshControl) {
refresher.endRefreshing()
myTableView.reloadData()
}
最佳答案
从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”,“spinner”,“loadingLabel”,也不需要“setLoadingScreen”和“removeLoadingScreen”功能。 UIRefreshControl将为您完成所有工作。让我知道是否有效。
稍后编辑(快速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()
})
})
}
在viewWillAppear中调用此函数。
并摆脱animateTable函数,除非您确实想要逐行设置动画效果,在这种情况下:
并在上一个动画完成后为每行设置动画,这是另一个主题。
希望对您有所帮助:)
编辑最后的编辑,翻译为Swift 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()
})
})
}