gpt4 book ai didi

swift - 如何从 firebase 异步加载数据并将其显示在 UICollectionView 中

转载 作者:行者123 更新时间:2023-11-30 10:53:06 24 4
gpt4 key购买 nike

我尝试显示已登录用户关注的用户的用户名。仅详细介绍那些在简单的 UICollectionView 中也有故事(如 Snapchat)的人。所以我需要该人的用户名在该单元格中显示为简单的标签。为了实现这一点,我认为我可以简单地将所有具有故事的内容添加到数组中,然后获取第一个故事的第一个项目,第二个故事的第二个项目,依此类推......但正如我之前提到的,我想检索并显示用户名。现在的问题是“cellForItemAt”函数没有等待从 firebase 检索到的数据填充数组,因此它使用空的 self.storyNames 数组以及 return self.storyNames.count 等于 0。

class HomeViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet weak var collectionView_stories: UICollectionView!

var storyNames: [String] = []

override func viewDidLoad() {
super.viewDidLoad()

collectionView_stories.delegate = self
collectionView_stories.dataSource = self
collectionView_stories.showsHorizontalScrollIndicator = false

getStoryNames() { storyNames in
self.storyNames = storyNames
DispatchQueue.main.async {
self.collectionView_stories.reloadData()
}
}
}

@IBAction func barButton_camera_pressed(_ sender: Any) {
}

@IBAction func barButton_inbox_pressed(_ sender: Any) {
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.storyNames.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let story_cell = collectionView_stories.dequeueReusableCell(withReuseIdentifier: "stories_cell", for: indexPath) as? StoryCollectionViewCell

story_cell?.imageView_story.layer.cornerRadius = (story_cell?.imageView_story.frame.size.width)! / 2
story_cell?.imageView_story.clipsToBounds = true
story_cell?.imageView_story.layer.shadowColor = UIColor.black.cgColor
story_cell?.imageView_story.layer.shadowRadius = 3
story_cell?.imageView_story.layer.shadowOpacity = 0.6
story_cell?.imageView_story.layer.borderWidth = 1.0
story_cell?.imageView_story.layer.borderColor = UIColor.darkGray.cgColor
story_cell?.imageView_story.image = UIImage(named: "bitmoji")

story_cell?.label_username.text = self.storyNames[indexPath.row]

return story_cell!
}

func getStoryNames(completion: @escaping ([String]) -> ()) {
var tempStoryNames: [String] = []
let userID = Auth.auth().currentUser?.uid
Database.database().reference().child("subscriptions").child(userID!).observeSingleEvent(of: .value) { snapshot in
let dispatchGroup = DispatchGroup()
for child in snapshot.children.allObjects as! [DataSnapshot] {
let dict = child.value as! String
Database.database().reference().child("users").child(dict).child("hasStory").observeSingleEvent(of: .value) { snapshot in
if let item = snapshot.value as? Bool {
if (item == true) {
dispatchGroup.enter()
Database.database().reference().child("users").child(dict).child("username").observeSingleEvent(of: .value) { snapshot in
let aaa = snapshot.value as! String
tempStoryNames.append(aaa)
print(tempStoryNames)
}
}
}
dispatchGroup.leave()
}
}
dispatchGroup.notify(queue: .main) {
completion(tempStoryNames)
}
}
}
}

您知道如何解决这个问题吗?我已尽力而为,也知道这不是最好的代码,但我正在努力,因此我真的需要您的帮助。我感谢每一个答案!

最佳答案

numberOfItemsInSection中,您应该依赖于storyNames的长度,这将确保您的数据正确显示

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.storyNames.count
}

关于swift - 如何从 firebase 异步加载数据并将其显示在 UICollectionView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54246316/

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