swift - 如何从 firebase 仅检索 UID 并在 TableView 中显示

标签 swift firebase

现在它从数据库中提取所有信息。我希望它只从数据库中具有 storename 的用户中提取信息。

我可以检索所有内容,但哪一个没有 storename 它将在我的应用程序中显示空表。

我的 Firebase 数据库 database 我的表格 View my table

我的代码如何检索它

override func viewDidLoad() {
        super.viewDidLoad()


        let refList = Database.database().reference().child("Users/Sellers")
        refList.observe(DataEventType.value, with:{(snapshot) in
            if snapshot.childrenCount>0{
                self.listProduct.removeAll()

                for lists in snapshot.children.allObjects as! [DataSnapshot]{
                    let productList = lists.childSnapshot(forPath: "user_info")
                    let userList = productList.value as? [String: AnyObject]
                    let listName = userList?["storename"]
//                    print("the Key value  \(lists.key) " )
                    print("key = \(self.shops)")
                    let list = StoreModel(name: listName as! String?)
                    self.shops.append(Shop(storename: listName as! String?, shop_id: lists.key ))
                    self.listProduct.append(list)
                }
                self.tableList.reloadData()
            }

        })

    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return  UITableView.automaticDimension
    }

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

    }

我希望它只从数据库中具有 storename 的用户中提取信息并显示在我的表格 View 中。

最佳答案

你可以试试

refList.observe(.value, with:{(snapshot) in
    if snapshot.childrenCount > 0 {
        self.listProduct.removeAll()

        for lists in snapshot.children.allObjects as! [DataSnapshot]{
            let productList = lists.childSnapshot(forPath: "user_info")
            let userList = productList.value as? [String: AnyObject]
            if let listName = userList?["storename"] as? String {
                let list = StoreModel(name: listName)
                self.shops.append(Shop(storename: listName, shop_id: lists.key ))
                self.listProduct.append(list)

            }
        }
        self.tableList.reloadData()
    }

})

关于swift - 如何从 firebase 仅检索 UID 并在 TableView 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56239059/

相关文章:

swift - KVO : How to get old/new values in observeValue(forKeyPath:. ..) 在 Swift 中?

node.js - TypeError : firestoreService. snapshot_ 不是函数

ios - Xib 文件内的 TableView

ios - 可选类中的可选属性 VS 可选字典中的可选值

swift - Swift 2.0 中的 PFFile 图像缓存

ios - 错误 : could not find an overload for'init'that accepts the supplied arguments double (swift)

javascript - 错误类型错误 : Cannot set property 'product' of null

javascript - Firebase 数据关系和选择的最佳实践

swift - 导航回 tableview View Controller 时更新 tableview 而不是整个重新加载

javascript - 如何在 HTML 表格上显示带有多级键的 Firebase 数据?