【问题标题】:The table view does not display the node post in firebase database表视图不显示firebase数据库中的节点帖子
【发布时间】:2017-10-13 01:23:22
【问题描述】:

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

帖子:

可选(“hXCdzourFJNRLCH64GF7yZVzu0I3”) -Kj_auPDxdboKTgPV-9J 文字:你好

  • 这是在 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)
    }
}

}

这是表格视图代码:

@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
}

【问题讨论】:

  • 那个节点在哪里设置?
  • 节点正在firebase中设置我不明白你的问题
  • @PeterTao 这是设置firebase节点帖子的代码检查问题中的最后更新

标签: ios uitableview firebase swift3


【解决方案1】:

尝试添加:

feeds.reloadData()

self.ac.stopAnimating()之前

你没有理由打电话给self

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

【讨论】:

  • 当我从 loogedinuserdata 中删除 self 时,会发生错误,提示插入 self,但它适用于数据库 ref 的第一个实例
  • self.feeds.reloaddata 没有改变任何东西是否存在解包问题导致firebase在节点@GlennHerping之前写入可选
  • 它只适用于登录用户数据的第一个实例。 :) 插入行之前print(posts[Insert index here]["text"]) 的输出是什么?
  • 输出为空 self.databaseref.child("posts").child(self.loggedinuser!.uid).observe(.childAdded, with: {( snapshot: FIRDataSnapshot) in this line posts return 0 我也认为这是由于可选数据
【解决方案2】:

问题在于有一个可选的登录用户:self.loggedinuser = FIRAuth.auth()?.currentUser。改成

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

【讨论】:

  • 我在 tableview 和 post view 控制器中更改了它,但我仍然得到返回 nil 的可选数据
  • 问题出在节点的key上,不是tableView的代码。要修复它,请使用登录用户更新节点的值(参见上面的代码)。
  • 我更改了它,但发生了“发生内部错误。编辑功能可能受到限制”
  • 可能只需要清除派生数据即可。看到这个问题:stackoverflow.com/questions/33456411/…
  • 我删除并重新启动计算机,然后错误消失了,我正在处理旧错误
猜你喜欢
  • 2019-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-30
相关资源
最近更新 更多