按下按钮时出现运行时错误

时间:2017-08-14 18:47:45

标签: ios swift uitableview firebase firebase-realtime-database

我只是想在帖子的关键字下保存喜欢的帖子和喜欢的人数

无效更新:第0节中的行数无效。更新后的现有部分中包含的行数(1)必须等于更新前该部分中包含的行数(1),或者减去从该部分插入或删除的行数(插入1个,删除0个),加上或减去移入或移出该部分的行数(0移入,0移出)。 Here is an image

 @IBAction func didtaplike(_ sender: Any) {

    print("like tappppppppped")
    self.loggedinuser = Auth.auth().currentUser
    let key =  ref.child("posts").childByAutoId().key

    ref.child("posts").child(self.loggedinuser!.uid).observeSingleEvent(of: .value, with: { (snapshot) in
        if let post = snapshot.value as? [String : AnyObject] {
            print(post)
          let updatelikes :[String:Any] = ["userswholiked/\(key)" : Auth.auth().currentUser!.uid]
             self.ref.child("posts").child(self.loggedinuser!.uid).updateChildValues(updatelikes)


        }


    })


  }

这是在主视图控制器中,类似按钮位于tableview单元格

       override func viewDidLoad() {

          super.viewDidLoad()

    self.ac.startAnimating()

    self.loggedinuser = Auth.auth().currentUser

    self.databaseref.child("users").child(self.loggedinuser!.uid).observeSingleEvent(of: .value) {
        (snapshot:DataSnapshot) in

        let  snao = snapshot.value as! [String:AnyObject]
        if (snao["pc"] != nil) {
        self.x = snao["pc"] as! String
        }

        if let t    = snao as AnyObject?
        {
            self.loggedinuserdata = t
        }

        //  print(self.loggedinuserdata)

        self.databaseref.child("posts").child(self.loggedinuser!.uid).observe(.childAdded, with: {( snap: DataSnapshot) in

            let snapp = snap.value as! [String:AnyObject]
            if let x = snapp["text "] as! String? {
                self.posts.insert(x, at: 0)

            }

            self.feeds.insertRows(at: [IndexPath(row : 0 ,section : 0)], with: UITableViewRowAnimation.automatic)
            self.feeds.reloadData()

            self.ac.stopAnimating()
        }

        )}


}

0 个答案:

没有答案