gpt4 book ai didi

ios - 使用UIRefreshControl刷新tableview

转载 作者:行者123 更新时间:2023-12-01 19:37:31 26 4
gpt4 key购买 nike

我正在使用refreshcontrol,但是在我的代码编译时,当前代码存在一个问题,即它先删除所有数据,然后再提取表数据,这会导致片刻不显示任何数据的问题。有快速解决方案吗?我应该在帖子加载后通过调用removeAll来使用完成处理程序吗?

编辑:我已经在下面添加了fetchNetworkPost的相关部分。

override func viewDidLoad() {
super.viewDidLoad()

let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(requestData), for: .valueChanged)

self.refreshControl = refreshControl

requestData()
}

@objc
func requestData() {

self.postArray.removeAll()
fetchNetworkPosts() //populates data

refresh()

}

func refresh() {

self.refreshControl?.endRefreshing()

self.tableView.reloadData()

}

func fetchNetworkPosts() {

for personId in networkArray {

let currentUserRef =
Database.database().reference().child("posts").queryOrdered(byChild:
"userId").queryEqual(toValue: personId)
currentUserRef.observeSingleEvent(of: .value, with: {
(snapshot) in

for childSnapshot in snapshot.children {

let test = Post(snapshot: childSnapshot as! DataSnapshot)

self.postArray.append(test)

}

self.postArray.sort { $0.postDate > $1.postDate } //sort

self.tableView.reloadData()

})

}
}

最佳答案

在完成fetchNetworkPosts()之后,调用您的refresh(),如下所示。检查下面的代码。

@objc func requestData() {
fetchNetworkPosts() //populates data
}

func refresh() {

self.refreshControl?.endRefreshing()
self.tableView.reloadData()

}

func fetchNetworkPosts() {

self.postArray.removeAll() // remove your array data here

for personId in networkArray {

let currentUserRef =
Database.database().reference().child("posts").queryOrdered(byChild:
"userId").queryEqual(toValue: personId)
currentUserRef.observeSingleEvent(of: .value, with: {
(snapshot) in

for childSnapshot in snapshot.children {

let test = Post(snapshot: childSnapshot as! DataSnapshot)

self.postArray.append(test)

}

self.postArray.sort { $0.postDate > $1.postDate } //sort

self.refresh()

})

}
}

关于ios - 使用UIRefreshControl刷新tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59573847/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com