Firebase更新 [英] Firebase update

查看:43
本文介绍了Firebase更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Firebase上相对较新.我想更新Firebase数据库上的现有数据.我的Xcode项目上有UITableView和UITableViewCells,当用户触摸(轻击手势)(例如,单元格视图上的插座)时,我想更新Firebase数据库,但这可以是tableview上的任何单元格.如何在Firebase数据库上找到用户在屏幕上触摸过的该单元格并更新其messageVoteScore值.分配了键,但是我的单元格不知道这些键,我不知道如何匹配它们.(数据库/消息/{"sender":"email"},{"messageBody":"text .."},{"messageVoteScore":"100"}

I am relatively new on firebase. I want to update existing data on my firebase database. I have UITableView and UITableViewCells on my Xcode project, when user touch (tap gesture), for example a outlet on cell view, I want to update Firebase Database but this could be any cell on tableview. How to find this cell which user touched on screen, on firebase database and update its messageVoteScore value. There are assigned keys but my cells do not know those keys, I could not figure it out how to match them.(Database/Messages/{"sender": "email"},{"messageBody":"text.."},{"messageVoteScore":"100"}

@objc func voteUpTapped(sender: UITapGestureRecognizer) {

    // Update score //
    self.messageVoteScore.text = String(Int(self.messageVoteScore.text!)! + 1 )

    //observeSingleEventOfType listens for a tap by the current user.
    Database.database().reference().child("Messages").observe(.value){
        (snapshot) in

        if let snapshotValue = snapshot.value as? [String : String] {
            print(snapshotValue)
        }
    }
}

推荐答案

您要将类对象传递到您的单元格吗?如果是这样,您可以为此使用协议/代理.UITableView单元格是一个视图,不应用作控制器.您的UITableViewController应该正在更新Firebase.

Are you passing class objects to your cell? If so you could use a protocol/delegate for this. A UITableView Cell is a View and shouldn't function as a controller. your UITableViewController should be updating firebase.

您应该首先在Cell类的顶部添加以下内容:

you should start by adding something like this to the top of your Cell class:

    protocol MyTableViewCellDelegate: class {
        func thingDidDo(object: Object, text: String...) //object is the class object you are changing.

然后将委托添加到您的属性中:

then add the delegate to your properties:

    weak var delegate: MyTableViewCellDelegate?

然后添加IBAction:

then add the IBAction:

    @IBAction func thingDidDo(_ sender: Any) {
        delegate?.thingDidDo(object: object, text: textView.text...)
    }

现在回到您的viewController类:

Now back to your viewController Class:

在您的"cellForRowAt"中输入委托:

In your "cellForRowAt" write in the delegate:

    cell.delegate = self

然后添加扩展名

    extension MyTableViewController: MyTableViewCellDelegate {

       func thingDidDo(object: Object, text: String) {
          Do whatever you want with object, text...
       }

不确定所要执行的功能是什么,因此我将其设为通用

Not sure what kind of function you are trying to perform so I made this as generic as I could

这篇关于Firebase更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆