在tableViewCell

时间:2019-03-20 13:50:35

标签: ios swift firebase uitableview firebase-realtime-database

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

我的firebase JSON结构的一个片段是:

 "followers" : {
    "FoFQDAGGX9hntBiBdXYCBHd8yas2" : {
      "CjeP35ceAQZJuUPhm7U1eF3Yq4F3" : true,
      "FjS4wUpXAUa5aWwXkjvujHxE4He2" : true,
      "Gmg1ojNoBiedFPRNSL4sBZz2gSx2" : true,
      "PqMkClaPM3W8k7ZSgzAHb3yne5D3" : true,
      "buS4recuDpdg60ckFqwjoU344TC2" : true
    },
 "users" : {
    "CjeP35ceAQZJuUPhm7U1eF3Yq4F3" : {
      "email" : "bbbb@gmail.com",
      "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"))
    }
}

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

@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
    }
}

1 个答案:

答案 0 :(得分: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()