从 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)
在刷新数据中:
//get the new data
refresher.endRefreshing()
tableView.reloadData()
您不需要“loadingView”、“spinner”、“loadingLabel”,也不需要“setLoadingScreen”和“removeLoadingScreen”函数。 UIRefreshControl 将为您完成所有工作。让我知道它是否有效。
稍后编辑(Swift 2)
如果您还想在第一次显示表格时以编程方式启动刷新控件,并在您获取数据时让它运行:
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 函数,除非你真的想要逐行动画的效果,在这种情况下:
你要么让它保持原样,它就不会很明显(实际上,根本不可见......),但它会稍微减慢你的 UI
或者,如果您想花时间并且想用响应性换取特殊效果,则必须设置完成处理程序
并在上一个完成动画之后为每一行制作动画,这是另一个主题。
希望对你有帮助:)
EDIT 上一次编辑,翻译成 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()
})
})
}