表视图不显示firebase数据库中的节点帖子

时间:2017-05-14 01:18:34

标签: ios uitableview firebase swift3

这是firebase节点的帖子,我不知道为什么它是可选的。我认为这是问题所在。

帖子:

  

可选(" hXCdzourFJNRLCH64GF7yZVzu0I3&#34)    -Kj_auPDxdboKTgPV-9J    text:你好

  • 这是在firebase数据库中设置节点的get

@IBOutlet weak var post: UIButton! @IBOutlet weak var newpost: UITextView!

var databaseref = FIRDatabase.database().reference()
var loggedinuser : AnyObject?

override func viewDidLoad() {
    super.viewDidLoad()
    self.loggedinuser = FIRAuth.auth()?.currentUser
    newpost.textContainerInset = UIEdgeInsetsMake(30, 20, 20, 20)
    newpost.text = "what is new?"
    newpost.textColor = UIColor.lightGray
}

@IBAction func didtapcancel(_ sender: Any) {
    dismiss(animated: true, completion: nil)
}

func didbeganediting(_ textView : UITextView)  {
    if(newpost.textColor == UIColor.lightGray){
        newpost.text = ""
        newpost.textColor = UIColor.black
    }
}
func textFieldShouldReturn(_ textfield: UITextField) -> Bool {
    textfield.resignFirstResponder()
    return false
}

func didtappost(_ sender: Any) {
    if (newpost.text.characters.count>0) {
        let key = databaseref.child("posts").childByAutoId().key
        let childupdates = ["/posts/\(self.loggedinuser!.uid)/\(key)/text ": newpost.text,"/time/\(self.loggedinuser!.uid)/\(key)/text":"\(Date().timeIntervalSince1970)"] as [String : Any]
        self.databaseref.updateChildValues(childupdates)
        dismiss(animated: true, completion: nil)
    }
}

}

这是tableview代码:

@IBOutlet weak var ac: UIActivityIndicatorView!
@IBOutlet weak var feeds: UITableView!

var databaseref = FIRDatabase.database().reference()
var loggedinuser : AnyObject?
var loggedinuserdata : NSDictionary?
var posts = [NSDictionary]()

override func viewDidLoad() {
    super.viewDidLoad()
    self.loggedinuser = FIRAuth.auth()?.currentUser
    self.databaseref.child("users").child(self.loggedinuser!.uid).observeSingleEvent(of: .value) { (snapshot:FIRDataSnapshot) in
        self.loggedinuserdata = snapshot.value as? NSDictionary
        self.databaseref.child("posts").child(self.loggedinuser!.uid).observe(.childAdded, with: { (snapshot: FIRDataSnapshot) in
            self.posts.append(snapshot.value as! NSDictionary)
            self.feeds.insertRows(at: [IndexPath(row : 0 ,section : 0)], with: UITableViewRowAnimation.automatic)
            self.ac.stopAnimating()
        }) {(error) in
            print(error.localizedDescription)
        }
    }
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

public  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return  self.posts.count
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell : viewTableViewCell = tableView.dequeueReusableCell(withIdentifier: "opa", for:indexPath) as! viewTableViewCell
    let post = posts[(self.posts.count-1) - indexPath .row]["text"] as! String

    cell.configure(nil, name:self.loggedinuserdata!["name"]  as! String          
    handle: self.loggedinuserdata!["handle"]  as! String, post: post)

    return cell
}

2 个答案:

答案 0 :(得分:0)

尝试添加:

feeds.reloadData()

之前self.ac.stopAnimating()

您没有理由打电话给self

  • self.loggedinuserdata
  • self.databaseref(第一个实例)

答案 1 :(得分:0)

问题是有一个可选的登录用户:self.loggedinuser = FIRAuth.auth()?.currentUser。将其更改为

self.loggedinuser = FIRAuth.auth()!.currentUser