根据返回的 queryEqual(toValue) 填充表视图

populate tableview based on returned queryEqual(toValue)

我在基于快照键填充 tableView 时遇到问题。控制台正在打印从 queryEqual(toValue: locationString) 调用的 parent 节点和 children 的所有 (nodeToReturn) 值,因此我知道我正在正确查询它们。但出于某种原因,我的 tableView 不断填充我的 Database.database().reference().child("Travel_Experience_Places") 中的所有用户词典。我只希望 tableView 显示来自 "nodeToReturn" 值的快照数据,所以不是来自我的 "Travel_Experience_Places" 数据库引用的每个 parent 节点 - 只有 parent 节点具有相同的"locationString"在其children中的价值。希望这是有道理的。提前致谢!

//模型object

class User: SafeUserObject {

    var id: String?
    var name: String?
    var email: String?
    var profileImageUrl: String?
    var place: String?

    init(dictionary: [String: AnyObject]) {
        super.init()

        self.id = dictionary["fromId"] as? String
        self.name = dictionary["addedBy"] as? String
        self.email = dictionary["email"] as? String
        self.profileImageUrl = dictionary["profileImageUrl"] as? String
        self.place = dictionary["place"] as? String
        setValuesForKeys(dictionary)
    }
}

// JSON 结构

  "Travel_Experience_Places" : {
    "-Ks0Ms4fEQZxBytI-vu_" : {
      "addedBy" : "Daniel Meier",
      "place" : "Barcelona, Spain",
      "profileImageUrl" : "https://firebasestorage.googleapis.com/v0/b/travelapp-255da.appspot.com/o/profile_images%2F21673E85-4F58-480D-B0A9-65884CADF7B3.png?alt=media&token=9e111014-d1d7-4b3b-a8c2-3897032c34cc",
      "timestamp" : 1.503261589872372E9
    },

// tableview 到 return firebase queryEqual(toValue: locationString) in tableview

var experiencePlaceUsers = [User]()

    func fetchTravelingUserCell() {

        let databaseRef = Database.database().reference().child("Travel_Experience_Places")
        databaseRef.queryOrdered(byChild: "place").queryEqual(toValue: locationString).observe(.value, with: { snapshot in

            if snapshot.exists(){
                let allKeys = snapshot.value as! [String : AnyObject]
                let nodeToReturn = allKeys.keys
                print(nodeToReturn) // prints parent and child values that have locationString as a child value in its data structure
            }
        })

        databaseRef.observe(.childAdded, with: { (snapshot) in

            if let dictionary = snapshot.value as? [String: AnyObject] {
                let user = User(dictionary: dictionary)
                self.experiencePlaceUsers.append(user)

                self.tableView.reloadData()
            }
        }, withCancel: nil)
    }

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! UserCell
    cell.user = experiencePlaceUsers[indexPath.item]

    return cell
}

我认为您需要在获取 nodeToReurn 的块中重新加载表视图

var experiencePlaceUsers = [User]()

    func fetchTravelingUserCell() {



    let databaseRef = Database.database().reference().child("Travel_Experience_Places")
                databaseRef.queryOrdered(byChild: "place").queryEqual(toValue: locationString).observe(. childAdded, with: { snapshot in



                if snapshot.exists(){
                    if let allKeys = snapshot.value as? [String: AnyObject] {
                        let singleUser = User(dictionary: allKeys)
                        self.experiencePlaceUsers.append(singleUser)


                        DispatchQueue.main.async(execute: {
                           self.tableView.reloadData()
                        })
                   }
                }
            }) 


   /*
        databaseRef.observe(.childAdded, with: { (snapshot) in

            if let dictionary = snapshot.value as? [String: AnyObject] {
                let user = User(dictionary: dictionary)
                self.experiencePlaceUsers.append(user)

                self.tableView.reloadData()
            }
        }, withCancel: nil)
*/
    }