gpt4 book ai didi

swift - tableview 不显示 firebase 用户

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

我的表格 View 没有显示原型(prototype)单元格时遇到问题。我已经正确命名了每个单元格和标识符。这是我当前的代码:(谢谢!)

import UIKit
import Firebase
import FirebaseDatabase
class UsersTableViewController: UITableViewController {



@IBOutlet var tableview: UITableView!

var ref: DatabaseReference!

var user = [User]()


override func viewDidLoad() {
super.viewDidLoad()
getusers()
// Do any additional setup after loading the view.
}

func getusers() {
let ref = Database.database().reference()

ref.child("users").child(Auth.auth().currentUser!.uid).queryOrderedByKey().observeSingleEvent(of: .value, with: { (snapshot) in

let users = snapshot.value as? [String : AnyObject] ?? [:]

for (_, value) in users
{
if let uid = users["uid"] as? String
{
if uid != Auth.auth().currentUser!.uid
{
let showUser = User()

if let fullname = users["fullname"] as? String, let imagePath = users["urlImage"] as? String
{
showUser.fullname = fullname
showUser.imagePath = imagePath
showUser.userID = uid
self.user.append(showUser)
}

}


}

}
self.tableview.reloadData()
})

ref.removeAllObservers()
}

override func numberOfSections(in tableview: UITableView) -> Int {
return 1
}

override func tableView(_ tableview: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! UserCell

cell.nameLabel.text = self.user[indexPath.row].fullname
cell.UserID = self.user[indexPath.row].userID
cell.userImage.downloadImage(from: self.user[indexPath.row].imagePath!)
checkFollowing(indexPath: indexPath)

return cell
}

override func tableView(_ tableview: UITableView, numberOfRowsInSection section: Int) -> Int {
return user.count
}


override func tableView(_ tableview: UITableView, didSelectRowAt indexPath: IndexPath) {

let uid = Auth.auth().currentUser!.uid
let ref = Database.database().reference()
let key = ref.child("users").childByAutoId().key

var isFollower = false

ref.child("users").child(uid).child("following").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in

if let following = snapshot.value as? [String : AnyObject] {
for (ke, value) in following {
if value as! String == self.user[indexPath.row].userID {
isFollower = true

ref.child("users").child(uid).child("following/\(ke)").removeValue()
ref.child("users").child(self.user[indexPath.row].userID).child("followers/\(ke)").removeValue()

self.tableview.cellForRow(at: indexPath)?.accessoryType = .none
}
}
}
if !isFollower {
let following = ["following/\(key)" : self.user[indexPath.row].userID]
let followers = ["followers/\(key)" : uid]

ref.child("users").child(uid).updateChildValues(following)
ref.child("users").child(self.user[indexPath.row].userID).updateChildValues(followers)

self.tableview.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
})
ref.removeAllObservers()

}


func checkFollowing(indexPath: IndexPath) {
let uid = Auth.auth().currentUser!.uid
let ref = Database.database().reference()

ref.child("users").child(uid).child("following").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in

if let following = snapshot.value as? [String : AnyObject] {
for (_, value) in following {
if value as! String == self.user[indexPath.row].userID {
self.tableview.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
}
}
})
ref.removeAllObservers()

}



override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}

我目前正在使用 Google 的 Firebase API 进行用户存储。在这段代码中,我试图将用户获取到我的 TableView ,只有我的 TableView 没有显示任何信息。如果您能提供帮助,在此先感谢您!

最佳答案

删除 ref.removeAllObservers() 行。方法 observeSingleEvent 在完成执行后自动移除观察者。所以在你的情况下 ref.removeAllObservers() 可以在完成调用之前删除它们,因此完成中的任何代码都不会执行。

关于swift - tableview 不显示 firebase 用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47895005/

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