2017-03-16 60 views
0

尝试查询Firebase子项并将其数据的快照数组检索到tableview中。不知道我是否正确实施,但我不是运行时错误。但是,我的tableview只是白色的,没有任何对象显示。一些反馈会有帮助。谢谢。检索Firebase子项并将它们填充到UITableView中

这是我的FB JSON树结构

Here is my FB JSON tree structure.

这是我的用户类(VAR USERLIST =用户)

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

} 
+0

你试过断点吗?你有“打印”,你看你的控制台? –

+0

@Lu_是的,没有打印语句在控制台中执行 –

+0

@ KyleC.Beachem您希望Firebase中的所有记录不是您在图像中显示的单个记录。 –

回答

1

可能是问题出在你的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() 
}) 
+0

libC++ abi.dylib:以NSException类型的未捕获异常终止 (lldb) –

+0

@ KyleC.Beachem检查编辑后的答案以及您遇到此崩溃的位置? –

+0

这里[链接](http://gdurl.com/ET-m) –

0

简单的问题。你有没有委托你的tableview?

class YourController: <other>, UITableViewDataSource, UITableViewDelegate { 
    @IBOutlet weak var tableView: UITableView! 

    override func viewDidLoad() { //for example this function 
     super.viewDidLoad() 

     self.tableView.delegate = self 
     self.tableView.dataSource = self 
} 
+0

是的,看看编辑。我正在使用UITableViewController。 –

相关问题