从Firebase孩子那里获取数据

时间:2018-07-09 06:47:23

标签: ios swift xcode

我在一个表视图中有两个自定义单元格。

 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的样子 enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

  1. 第一件事是将firebase调用分为一个单独的函数,然后在该函数中填充一个字典,其中以posts作为键,并将注释作为值

例如这样的

var postComments: [String: [String]] = [:] // post as key and string array as comments
  1. 在firebase数据库调用中,使用快照填充此内容。

在数据调用之后但在Firebase数据库回调中,请使用此功能重新加载数据

DispatchQueue.main.async{
  tableView.reloadData() 
}
  1. 使用postComments数组设置单元格。