显示uitableViewCell上子节点的所有数据

问题描述 投票:3回答:1

我无法在桌面视图单元格上显示用户的所有关注者及其个人资料图片和全名(类似于instagram)。

我的firebase JSON结构的片段是:

 "followers" : {
    "FoFQDAGGX9hntBiBdXYCBHd8yas2" : {
      "CjeP35ceAQZJuUPhm7U1eF3Yq4F3" : true,
      "FjS4wUpXAUa5aWwXkjvujHxE4He2" : true,
      "Gmg1ojNoBiedFPRNSL4sBZz2gSx2" : true,
      "PqMkClaPM3W8k7ZSgzAHb3yne5D3" : true,
      "buS4recuDpdg60ckFqwjoU344TC2" : true
    },
 "users" : {
    "CjeP35ceAQZJuUPhm7U1eF3Yq4F3" : {
      "email" : "[email protected]",
      "fullname" : "Bbbb",
      "profileImageUrl" : "https://firebasestorage.googleapis.com/v0/b/pinion-4896b.appspot.com/o/profile_image%2FCjeP35ceAQZJuUPhm7U1eF3Yq4F3?alt=media&token=0449c633-b397-4452-b2df-41f3a5390084",
      "work" : "Nottingham",
    },

表视图单元格中的代码(FollowersTableViewCell):

@IBOutlet weak var followersProfileImage: UIImageView!
@IBOutlet weak var followersNameLabel: UILabel!

var user: UserModel? {
    didSet {
        updateView()
    }
}
func updateView() {
    followersNameLabel.text = user?.fullname
    if let photoUrlString = user?.profileImageUrl {
        let photoUrl = URL(string: photoUrlString)
        followersProfileImage.sd_setImage(with: photoUrl, placeholderImage: UIImage(named: "placeholderImg"))
    }
}

编辑:视图控制器中的代码(Followers ViewController)

@IBOutlet weak var tableView: UITableView!

var users: [UserModel] = []

func loadusers() {
    let ref = Database.database().reference()
    guard let currentUser = Auth.auth().currentUser?.uid else { return }

    var followersNames = [String]()
    var profileImage = [String]()

    let followersRef = ref.child("followers").child(currentUser) //retreives all nodes in the following node
    followersRef.observe(DataEventType.value, with: { snapshot in
        print(snapshot.children.allObjects)
        for child in snapshot.children { //build the array of keys
            let snap = child as! DataSnapshot
            let key = snap.key

            let userRef = ref.child("users").child(key) //get the user name and profile image from the users node
            userRef.observeSingleEvent(of: .value, with: { snapshot in
                let followersName = snapshot.childSnapshot(forPath: "fullname").value as! String
                let followersProfileImageUrl = snapshot.childSnapshot(forPath: "profileImageUrl").value as! String
                print(followersName)
                print(followersProfileImageUrl)

                followersNames.append(followersName)
                profileImage.append(followersProfileImageUrl)
                self.tableView.reloadData()
            })
        }
    })
}
extension FollowersViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return users.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "FollowersTableViewCell", for: indexPath) as! FollowersTableViewCell
    let user = users[indexPath.row]
    cell.user = user
    return cell
  }
}

现在代码运行,关注者的个人资料图片和全名都打印在控制台上,但在应用程序的表格视图中没有显示任何内容 - 在此先感谢:)

更新:用户模型定义

class UserModel {
    var email: String?
    var work: String?
    var profileImageUrl: String?
    var fullname: String?
    var id: String?
}
extension UserModel {
    static func transformUser(dict: [String: Any], key: String) -> UserModel {
        let user = UserModel()
        user.email = dict["email"] as? String
        user.work = dict["work"] as? String
        user.profileImageUrl = dict["profileImageUrl"] as? String
        user.fullname = dict["fullname"] as? String
        user.id = key
        return user
    }
}
ios swift firebase uitableview firebase-realtime-database
1个回答
3
投票

您的TableView不显示任何数据,因为您不会在任何时候填充users数组。

我可能想在observeSingleEvent实现中实例化一个UserModel对象,将对象添加到users数组并在此之后调用reloadData(或insertRows)方法。 (而不是在实现块之外)

根据要求,这是一种快速(和脏)的方式来创建用户对象并刷新UI

let user = UserModel()
user.fullname = snapshot.childSnapshot(forPath: "fullname").value as? String
user.profileImageUrl = snapshot.childSnapshot(forPath: "profileImageUrl").value as? String

self.users.append(user)
self.tableView.reloadData()
© www.soinside.com 2019 - 2024. All rights reserved.