Swift Firebase就像表视图中的帖子一样

时间:2017-12-16 23:48:51

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

我正在使用Firebase在Swift中实施问答解答程序。我希望在tableViewCell中有一个类似的按钮。但是,我遇到了问题,因为post的数据在tableView类中,我可以对tableViewCell类中的like按钮进行更改。我需要在这两者之间进行数据传输。任何人都可以帮我这个吗?

如果它能帮助您理解我的问题,请在表格视图中输入代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let answerCell = tableView.dequeueReusableCell(withIdentifier: "AnswerCell") as! AnswerCell

        let answer = answers[indexPath.row]

        answerCell.answerText.text = answer.answerText
        answerCell.username.text = "~ \(answer.username)"
        answerCell.numberOfLikes.text = "\(answer.numberOflikes) liked this answer."

        answerCell.answerText.numberOfLines = 0

        return answerCell
    }

我希望在我的表格视图单元格中有这样的代码。但是,我无法访问答案对象的数据。

 @IBAction func likeButtonClicked(_ sender: UIButton) {
    ref = Database.database().reference()

    ref.child("answerLikes").child((answer.id).observeSingleEvent(of: .value, with: {
        (snapshot) in
        if let _ = snapshot.value as? NSNull {
            sender.setImage(UIImage(named: "filledHeart.png"), for: .normal)
        } else {
            answerLikes = [Auth.auth().currentUser?.uid : true]
            ref.child("answerLikes").child(answer.id).updateChildValues(answerLikes)
            sender.setImage(UIImage(named: "emptyHeart.png"), for: .normal)
        }
    })
}

1 个答案:

答案 0 :(得分:0)

<强>已更新

这是您提出的问题&#34;我无法访问答案对象的数据&#34;

您可以在answerCell中创建一个接收答案对象的方法。

#include "stdafx.h"
#include "Room.h"
#include <string>
using namespace std;

namespace mazeGraph
{
    Node::Node() : roomName(' '), north(NULL), south(NULL), east(NULL), west(NULL)
    {
    }
    Node::Node(char newNode) : roomName(newNode), north(NULL), south(NULL), east(NULL), west(NULL)
    {
    }
    char Node::getName() const
    {
        return roomName;
    }
    Node* Node::getAdjacentRoom(char direction) const
    {
        switch (direction)
        {
        case 'N':
            return north;
        case 'S':
            return south;
        case 'E':
            return east;
        case 'W':
            return west;
        }
        return NULL;
    }
    void Node::edge(char direction, Node * other)
    {
        switch (direction)
        {
        case 'N':
            north = other;
            break;
        case 'S':
            south = other;
            break;
        case 'E':
            east = other;
            break;
        case 'W':
            west = other;
            break;
        }
    }
    string Node::getMovementOptions()
    {
        string movement = "";
        if (north != NULL)
            movement = "N: North ";
        if (south != NULL)
            movement = "S: South ";
        if (east != NULL)
            movement = "E: East ";
        if (west != NULL)
            movement = "W: West ";
        return movement;
    }
}

//现在是时候从主UIViewController类中调用它来发送应答对象

#include "stdafx.h"
#include <string>
#include "Room.h"
#include "Room.cpp"
#include <iostream>

using namespace std;

int main()
{
    mazeGraph::nodeptr a, b, c, d, e, f, g, h, i, j, k, l;
    a = new Node('A');
    b = new Node('B');
    c = new Node('C');
    d = new Node('D');
    e = new Node('E');
    f = new Node('F');
    g = new Node('G');
    h = new Node('H');
    i = new Node('I');
    j = new Node('J');
    k = new Node('K');
    l = new Node('L');
    return 0;
}

现在,您将询问如何在这两个类之间进行数据传输

实现这一目标的第一种方式

1.在AnswerCell类中创建一个委托,并在你编写tableView代码的Controller中确认它。

2.单击按钮时,激活一个委托,您将在mainViewController中获得回调。

实现这一目标的第二种方式

1.在AnswerCell类中为likeButtonClicked制作一个IBOulet。

2.不要进行IBAction。

3.您的代码看起来像这样

func recieveAnswerObject(answer : Answer){
  // here you will get answer object
   // here i am assuming Answer is your model class
 }

并在tableview类中编写以下代码

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let answerCell = tableView.dequeueReusableCell(withIdentifier: "AnswerCell") as! AnswerCell

    let answer = answers[indexPath.row]
    // from here we send answer object
    answerCell.recieveAnswerObject(answer)
    answerCell.answerText.text = answer.answerText
    answerCell.username.text = "~ \(answer.username)"
    answerCell.numberOfLikes.text = "\(answer.numberOflikes) liked this answer."

    answerCell.answerText.numberOfLines = 0

    return answerCell
}