【发布时间】:2019-09-07 06:41:10
【问题描述】:
我有一个collectionView 和一个refreshControl
lazy var refreshControl: UIRefreshControl = {
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self,
action: #selector(handleRefreshControl(_:)),
for: .valueChanged)
return refreshControl
}()
//create collectionView
collectionView.refreshControl = refreshControl
我按下一个按钮从服务器获取一些数据。一旦进程开始,我使用refreshControl.refreshManually() 开始刷新。
getDataBarButton() {
refreshControl.refreshManually()
fetchData()
}
一旦检索到数据,我就会调用refreshControl.endRefreshing() 来停止刷新。
func fetchData() {
// got the data
refreshControl.endRefreshing()
}
所有这些都可以正常工作。这不是从数据库中提取更多数据的情况,因为只有按钮可以这样做。一旦我拉出简历,我想显示refreshControl,然后将其删除。我不想在完成后完全摆脱refreshControl,因为按钮可能会再次被按下。
问题是,一旦我拉出简历,refreshControl 就会开始刷新并且不会停止。它只是停留在那里。
我检查它是否停止了,但这不起作用:
@objc fileprivate func handleRefreshControl(_ sender: UIRefreshControl) {
if refreshControl.isRefreshing {
print("refreshing is occurring")
return
}
refreshControl.refreshManually()
refreshControl.endRefreshing()
}
在我看来,在返回数据并调用 after refreshControl.endRefreshing() 之后,一旦我将 cv 拉到 if refreshControl.isRefreshing 内的 print 语句 不应该 运行但它确实。
在创建和切换变量(如var isRefreshing = false/true)之外,如何检查refreshControl 是否在其target action 内刷新?
只是为了上下文,如果您正在阅读有关堆栈溢出 iOS 应用程序的这个问题,只需下拉并观察会发生什么。没有什么可刷新的,但是当拉动发生时,会显示一个刷新控件,然后将其删除。我想做同样的事情。
【问题讨论】:
-
这个
refreshControl.endRefreshing()应该在请求的完成处理程序中。 -
它确实在请求的完成处理程序中被调用。它发生在 firebase 回调中,我在 mainQueue 上调用它。我没有添加代码,因为我认为它与问题无关。不过谢谢
标签: ios swift uicollectionview uirefreshcontrol