ios - 检索 firebase 子项并将它们填充到 UITableView 中

标签 ios swift uitableview

Trying to query firebase children and retrieve a snapshot array of their data into a tableview. Not sure if I am implementing this correctly, but I am not the getting a runtime error. However, my tableview is just white with no objects displaying. Some feedback would be helpful. Thanks.

这是我的 FB JSON 树结构

Here is my FB JSON tree structure.

这是我的用户类(var userList = User)

Here is my User class (var userList = [User]())

class CDetailTableViewController: UITableViewController {


    static var imageCache = NSCache<AnyObject, UIImage>()

    var userList = [User]()

    var ref = FIREBASE.FBDataReference().ref

    var refHandle: UInt!



    override func viewDidLoad() {
        super.viewDidLoad()

        ref = FIREBASE.FBLink().FBref
        configureCell()
    }

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

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows


        return userList.count
    }


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

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! CDetailTableViewCell



        cell.priceLabel?.text = userList[indexPath.row].priceLabel
        cell.titleLabel?.text = userList[indexPath.row].titleLabel
        cell.itemPhoto?.image = userList[indexPath.row].objectImage

        return cell


    }


    func configureCell(){

        let ref = FIRDatabase.database().reference()

        let userID = FIRAuth.auth()?.currentUser?.uid

        refHandle = ref.child("Enterpriser Listings").child("Sell Old Stuff - Listings").child(userID!).observe(.childAdded, with: { (snapshot) in

            if let dictionary = snapshot.value as? [String : AnyObject]  {

                print("get dictionary")
                print(dictionary)

                let user = User()

                user.setValuesForKeys(dictionary)
                self.userList.append(user)

                // Get user value

                DispatchQueue.main.async {

                    print("reloaded")

                    self.tableView.reloadData()
                }


            }
        })

    }


    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // 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.
    }
    */

}

最佳答案

可能是您的 Firebase 引用有问题,请按此方式尝试。

ref.child("Enterpriser Listings").child("Sell Old Stuff - Listings").observe(.value, with: { (snapshot:FIRDataSnapshot) in
    var users = [User]()
    for child in snapshot.children {
        print("\((sweet as! FIRDataSnapshot).value)")
        if let dictionary = child.value as? [String : AnyObject]  {
            let user = User()
            user.setValuesForKeys(dictionary)
            users.append(user)
        } 
    }
    self.userList = users
    self.tableView.reloadData()
})

关于ios - 检索 firebase 子项并将它们填充到 UITableView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42832548/

相关文章:

iphone - iOS : Proper way to display required UITextField in UITableViewCell

ios - 使用 Parse 为 Swift 中的 iOS 应用程序创建您自己的数据库

ios - 让 AVAudioEngine 重复一个声音

ios - 如何将现有对象添加到 Parse.com Databrowser 中的关系中

ios - UICollectionView scrollToItem 在 iOS 14 中不起作用

Swift View Controller 向下转换

ios - 如何复合函数并持久化进度

ios - 表格单元格中标签的 sizeToFit 函数的意外行为

ios - UIRefreshControl 的拉动距离是如何计算的

ios - 更改 UItableView 高度后,我的滚动不再起作用。无法到达底部数据