gpt4 book ai didi

ios - 分段控件中的表格 View 不会立即显示数据

转载 作者:行者123 更新时间:2023-12-01 16:21:40 25 4
gpt4 key购买 nike

我有一个分段控件下的表格 View ,表格 View 的数据是从 Firebase 数据库中检索的,代码如下:

func retrieveParticipatedData(completion: @escaping (Bool) -> Void) {

let storyIDRef = DBRef.child("Story IDs").child(userID!)
var participatedStoryItems: [ParticipatedStoriesPageData] = []

storyIDRef.observeSingleEvent(of: .value) { (snapshot) in
if let dict = snapshot.value as? [String : String] {

for (_, value) in dict {

self.DBRef.child("To be reviewed stories").child(value).observeSingleEvent(of: .value) { (snapshot) in
if let storyDict = snapshot.value as? [String : Any] {

let storyTitle = storyDict["Book Title"] as? String
let storyDescription = storyDict["Description"] as? String
let storyID = value
let votes = storyDict["Votes"] as? Int

let story = ParticipatedStoriesPageData(storyKey: storyID, storyTitle: storyTitle!, storyDescription: storyDescription!, votes: votes!)
participatedStoryItems.append(story)
}
self.participatedStories = participatedStoryItems
completion(true)
}
}
}
}
}

override func viewDidLoad() {
super.viewDidLoad()

retrieveParticipatedData { (isRetreived) in

if isRetreived {
self.tableView.isHidden = false
self.tableView.reloadData()
}
}
}

数据的检索没有问题。表格 View 和分段控件最初是隐藏的,一旦检索到数据,我就会取消隐藏它们。

问题是表格 View 中的数据没有立即显示,我必须转到另一个选项卡并返回(我的分段控件中有 2 个选项卡),然后数据才会显示在表格 View 中.我还添加了数据库的图片。

Database

Participants database

更新

这是我在马特的帮助下尝试过的:
func retrieveParticipatedData() {

let dispatchGroup = DispatchGroup()
let storyIDRef = DBRef.child("Story IDs").child(userID!)
var participatedStoryItems: [ParticipatedStoriesPageData] = []

storyIDRef.observeSingleEvent(of: .value) { (snapshot) in
if let dict = snapshot.value as? [String : String] {
for (_, value) in dict {
self.DBRef.child("To be reviewed stories").child(value).child("Participants").child("Chapter 1 Author").observeSingleEvent(of: .value) { (snapshot) in

if snapshot.value as? String == self.userID {
dispatchGroup.enter()

self.DBRef.child("To be reviewed stories").child(value).observeSingleEvent(of: .value) { (snapshot) in
if let storyDict = snapshot.value as? [String : Any] {

let storyTitle = storyDict["Book Title"] as? String
let storyDescription = storyDict["Description"] as? String
let storyID = value
let votes = storyDict["Votes"] as? Int

let story = ParticipatedStoriesPageData(storyKey: storyID, storyTitle: storyTitle!, storyDescription: storyDescription!, votes: votes!)
participatedStoryItems.append(story)
}
self.participatedStories = participatedStoryItems.reversed()
dispatchGroup.leave()
}
dispatchGroup.notify(queue: .main) {
print("Retrieved")
self.tableView.reloadData()
}
// It works if I do this
/*dispatchGroup.notify(queue: .main) {
print("Retrieved")
self.setupTableView // Creates the table view
}*/
//But I don't wanna do this cuz then there would be no table view if there is no data retreived.
}
}
}
}
}
}

override func viewDidLoad() {
super.viewDidLoad()

retrieveParticipatedData()
setupStackView()
setupTableView() // Creates the table view
}

最佳答案

幽默我,试试这个。不要隐藏表格 View ,只需在 viewDidLoad() 中调用此函数即可。对于我的应用程序,我正在下载一长串用户并使用分段 Controller 加载个人资料照片的异步。我在这里可能是错的,因为我无法针对 firebase 对其进行测试,但这与我正在做的事情相似。

    func retrieveParticipatedData() {

let storyIDRef = DBRef.child("Story IDs").child(userID!)
var participatedStoryItems: [ParticipatedStoriesPageData] = []

storyIDRef.observeSingleEvent(of: .value) { (snapshot) in
if let dict = snapshot.value as? [String : String] {

for (_, value) in dict {

self.DBRef.child("To be reviewed stories").child(value).observeSingleEvent(of: .value) { (snapshot) in
if let storyDict = snapshot.value as? [String : Any] {

let storyTitle = storyDict["Book Title"] as? String
let storyDescription = storyDict["Description"] as? String
let storyID = value
let votes = storyDict["Votes"] as? Int

let story = ParticipatedStoriesPageData(storyKey: storyID, storyTitle: storyTitle!, storyDescription: storyDescription!, votes: votes!)
participatedStoryItems.append(story)
}
self.participatedStories = participatedStoryItems // should this be "append()" instead of "="? if not why not just call self.participatedStories.append(story) above instead?
self.tableView.reloadData()
}
}
}
}
}

关于ios - 分段控件中的表格 View 不会立即显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58357119/

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