Swift 数据仅附加到我的tableView和我的Firebase数据库一次
我正在尝试使用警报将来自一个ViewController(CreatePlaylistVC)的数据附加到我的tableView所在的另一个ViewController(CreatedPlaylistVC)中。我只能向tableView和Firebase数据库追加一次数据 我尝试了tableView.reloadData(),但这并没有起到作用 创建播放列表VCSwift 数据仅附加到我的tableView和我的Firebase数据库一次,swift,xcode,firebase,uitableview,Swift,Xcode,Firebase,Uitableview,我正在尝试使用警报将来自一个ViewController(CreatePlaylistVC)的数据附加到我的tableView所在的另一个ViewController(CreatedPlaylistVC)中。我只能向tableView和Firebase数据库追加一次数据 我尝试了tableView.reloadData(),但这并没有起到作用 创建播放列表VC alert.addAction(UIAlertAction(title:"OK", style:.default, handler: {
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()
}
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()
}
})
}
CreatedPlaylistVC
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()
}
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)