swift - 拉动刷新不适用于表格视图
问题描述
我的视图控制器中有一个表格视图,我正在尝试实现拉动刷新功能。当我拉刷新表视图不会重新加载并且数据不会更新。
当我在 fetchRefresh 函数中打印数组 print(tempRefresh) 时,它返回一个空数组?
我的 firebase 数据库看起来像这样。
override func viewDidLoad() {
super.viewDidLoad()
// refresh the page
refreshControl = UIRefreshControl()
if #available(iOS 10.0, *) {
tableView.refreshControl = refreshControl
} else {
// fallback on older versions
tableView.addSubview(refreshControl)
}
refreshControl.attributedTitle = NSAttributedString(string: "Loading...")
refreshControl.addTarget(self, action: #selector(handleRefresh), for: .valueChanged)
// reload the tabelview
tableView.reloadData()
tableView.delegate = self
tableView.dataSource = self
}
@objc func handleRefresh() {
print("refresh")
beginRefresh()
DispatchQueue.main.async {
self.refreshControl.endRefreshing()
}
}
func fetchRefresh(completion: @escaping(_ comments:[Comments])->()) {
oldCommentsQuery.queryLimited(toLast: 20).observeSingleEvent(of: .value, with: { snapshot in
var tempRefresh = [Comments]()
let commentsSnap = snapshot.childSnapshot(forPath: "comments")
let allComments = commentsSnap.children.allObjects as! [DataSnapshot]
for commentSnap in allComments {
let degree = commentSnap.childSnapshot(forPath: "reply degree").value as? String ?? ""
let name = commentSnap.childSnapshot(forPath: "reply name").value as? String ?? ""
let text = commentSnap.childSnapshot(forPath: "reply text").value as? String ?? ""
let university = commentSnap.childSnapshot(forPath: "reply university").value as? String ?? ""
let photoURL = commentSnap.childSnapshot(forPath: "reply url").value as? String ?? ""
let url = URL(string: photoURL)
let timestamp = commentSnap.childSnapshot(forPath: "timestamp").value as? Double
let lastComment = self.comments.last
if snapshot.key != lastComment?.id {
let newRefresh = Comments(id: snapshot.key, fullname: name, commentText: text, university: university, degree: degree, photoURL: photoURL, url: url!, timestamp: timestamp!)
tempRefresh.insert(newRefresh, at: 0)
//test
print(tempRefresh)
}
}
return completion(tempRefresh)
})
}
func beginRefresh() {
fetchingMore = true
// fetch the comments
fetchRefresh { newComments in
self.comments.append(contentsOf: newComments)
self.endReached = newComments.count == 0
self.fetchingMore = false
self.tableView.reloadData()
}
}
解决方案
推荐阅读
- java - 在 Java 中访问文本文件
- javascript - JS Array:过滤掉表中的列
- asp.net-core - 在提交/验证时在 ErrorMessage 中不使用 Blazor 表单中 InputNumber 控件上的 DisplayName
- html - 如何使这个备用滚动文本不超出其父元素的宽度或边?
- c - 在 C 中不使用 malloc 分配内存
- azure - 我们能否将复杂的 3 层应用程序发布到 Azure 市场
- javascript - 下一个 js url 正在更改,但部署到 vercel 后页面为白色
- mysql - 如何根据条件删除重复项?
- javascript - R 传单 - 使用其他 api 服务自定义瓷砖地图 (Goong Map)
- flask - Flask 在本地提供图像,但未部署到 Google App Engine