gpt4 book ai didi

swift - 数据仅一次附加到我的tableView和Firebase数据库中

转载 作者:行者123 更新时间:2023-11-28 07:20:53 25 4
gpt4 key购买 nike

我正在尝试使用警报将来自一个ViewController(CreatePlaylistVC)的数据附加到我的tableView所在的另一个ViewController(CreatedPlaylistVC)。我只能在tableView和Firebase数据库中追加一次数据。
我试过tableView.reloadData(),但这对我没用。
创建播放列表vc

alert.addAction(UIAlertAction(title:"OK", style:.default, handler: {
action in



if let playlistName = alert.textFields?.first?.text {


guard var userID = Auth.auth().currentUser?.uid else { return }

self.ref?.child("PlaylistName").child(userID).childByAutoId().setValue(playlistName)

self.tableView.reloadData()

}

创建播放列表vc
var ref:DatabaseReference?
var databaseHandle:DatabaseHandle?


override func viewDidLoad() {

//Set the firebase reference
ref = Database.database().reference()



guard var userID = Auth.auth().currentUser?.uid else { return }

databaseHandle = ref?.child("PlaylistName").child(Auth.auth().currentUser!.uid).observe(.childAdded, with: { (snapshot) in


//Try to covert the value of the data to a string
let post = snapshot.value as? String

if let actualPost = post {

//Append the data to our playlistNameArray
self.playlistNameArray.append(actualPost)

//Reload the tableView
self.tableView.reloadData()
}
})

}

我的预期结果是将我添加的每个项放在tableView中彼此的顶部或底部(最好放在彼此的顶部),并将多个项添加到我的数据库中。

最佳答案

试试这个(.value而不是.childadded)

databaseHandle = ref?.child("PlaylistName").child(Auth.auth().currentUser!.uid).observe(.value, with: { (snapshot) in

关于swift - 数据仅一次附加到我的tableView和Firebase数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58125122/

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