簡體   English   中英

從Firebase孩子那里獲取數據

[英]get data from firebase children

我在一個表視圖中有兩個自定義單元格。

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // Configure the cell...
     if (indexPath.row == 0) {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Main", for: indexPath) as! PostTableViewCell

        //Configure the cell

        cell.PostView.layer.cornerRadius = 5
        cell.PostView.layer.masksToBounds = false
        cell.PostView.layer.shadowColor = UIColor.black.withAlphaComponent(0.4).cgColor
        cell.PostView.layer.shadowOffset = CGSize(width: 0, height: 0)
        cell.PostView.layer.shadowOpacity = 0.9

        let post = Comments[indexPath.row] as! [String: AnyObject]
        let commentname = post["author"] as? String
        sendAuthor = post["author"] as? String
        cell.CommentersName.setTitle(commentname, for: .normal)

        if let seconds = post["pub_time"] as? Double {
            let timeStampDate = NSDate(timeIntervalSince1970: seconds/1000)
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "MMM d, yyyy"
            let formating = timeStampDate as Date


            cell.CommentTime.text = dateFormatter.string(from: formating)


        }

        cell.comment.text = post["content"] as? String


         textViewDidChange(cell.comment)

        cell.comment.frame.size.width = 344
        cell.comment.sizeToFit()
        cell.comment.clipsToBounds = true

        cell.REply.frame.origin.y = cell.comment.frame.maxY + 10
        cell.PostView.frame.size.height =  cell.comment.frame.maxY + 50
        TableView.rowHeight = cell.PostView.frame.size.height + 20


        cell.LikesNumber.text = post["num_likes"] as? String


        replyId = post["id"] as? String

        cell.checkfornightmode()

        return cell
       }
          else{

        let cell = tableView.dequeueReusableCell(withIdentifier: "Reply", for: indexPath) as! RepliesTableViewCell



            cell.ReplyCustomCell.layer.cornerRadius = 5
            cell.ReplyCustomCell.layer.masksToBounds = false
            cell.ReplyCustomCell.layer.shadowColor = UIColor.black.withAlphaComponent(0.4).cgColor
            cell.ReplyCustomCell.layer.shadowOffset = CGSize(width: 0, height: 0)
            cell.ReplyCustomCell.layer.shadowOpacity = 0.9

        let post = Comments[indexPath.row] as! [String: AnyObject]

        let posttest = post["id"] as? String

        let replyRef = Database.database().reference().child("main").child("posts").child(postID!).child("comments").child(posttest!).child("comments")


        replyRef.observeSingleEvent(of: .value, with: { (snapshot:DataSnapshot) in

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



                for testingkey in postsDictionary.keys {


                    Database.database().reference().child("main").child("posts").child(self.postID!).child("comments").child(posttest!).child("comments").child(testingkey).observeSingleEvent(of: .value, with: { (snapshot) in

                        let value = snapshot.value as? NSDictionary

                        let content : String? = value?["content"] as? String ?? ""

                       cell.ReplyText.text = content!

                    })
                }



            }

        })


        TableView.rowHeight = 150.0


            return cell
       }
    }

具有標識符main的第一個單元格應該打印出某個帖子的所有初始注釋。 帶有標識符的第二個單元格應該將注釋打印為主要注釋。 基於此代碼,我只會得到主要帖子的最后評論和主要帖子的最后評論。

這就是json的樣子 在此處輸入圖片說明 在此處輸入圖片說明

  1. 第一件事是將firebase調用分離到一個單獨的函數中,然后在該函數中填充一個字典,其中以posts作為鍵,並將comments作為值

例如這樣

var postComments: [String: [String]] = [:] // post as key and string array as comments
  1. 在firebase數據庫調用中,使用快照填充該內容。

在數據調用之后但在Firebase數據庫回調中,使用此函數重新加載數據

DispatchQueue.main.async{
  tableView.reloadData() 
}
  1. 用postComments數組設置單元格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM