1

私はFirebaseを使用してSwiftで質問& Answerプログラムを実装しています。私は私のテーブルのViewCellのようなボタンを持っています。しかし、ポストのデータがtableViewクラスにあり、tableViewCellクラスのlikeボタンを変更できるため、問題が発生しています。私はこれらの2つの間でデータ転送が必要です。誰もこれで私を助けることができますか?Swift Firebaseがテーブルビューの投稿のように

私のテーブルビュー内のコード、私の問題を理解するためにあなたを助けるために起こっている場合:

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) 
     } 
    }) 
} 
+0

をあなたの下のコードを書くのだろうか? –

+0

AnswerCellにあります。それはテーブルビューコントローラ内にあるはずですか?お返事をありがとうございます。 – atunaysaka

答えて

0

UPDATEDこれは、あなたが答えオブジェクトを受け取るanswerCellでメソッドを作成することができます

「私は答えオブジェクトのデータにアクセスすることはできません」尋ねたソリューションです。今

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

//あなたはどのようにこれらの二つのクラス

まず間のデータ転送に尋ねた部分のための今すぐ回答オブジェクト

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 
} 

を送信するために、あなたのメインのUIViewControllerクラスからそれを呼び出すための時間これを達成する方法

1. AnswerCellクラスの代理人を作成し、あなたのtableViewコードを書いたことをあなたのコントローラに確認してください。

2.デリゲートを起動すると、mainViewControllerでコールバックが発生します。 AnswerCellクラスのlikeButtonClickedためIBOulet 1.Makeこの

を達成するために

第二の方法。

2。IBActionを作成しないでください。

3.Yourコードは、この

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let answerCell = tableView.dequeueReusableCell(withIdentifier: "AnswerCell") as! AnswerCell 
    // action added to button 
    answerCell.likeButtonClicked.addTarget(self, action: #selector(likeButtonClicked), for: .touchUpInside) 
    // updated set tag to button 
    answerCell.likeButtonClicked.tag = indexPath.row 
    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 
} 

のようなものを見て、あなたのテーブルビュークラスのメソッド `likeButtonClicked`が配置されている

@IBAction func likeButtonClicked(_ sender: UIButton) { 
//updated now tag is row position of button in cell and is equal to  (indexPath.row) of that particular button.tag is equal to 
let tag = sender.tag 
// here indexPath.row can be replace by tag so 
    //let answer = answers[indexPath.row] == let answer = answers[tag] 
    // updated till here 
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) 
    } 
}) 
    } 
+0

あなたの応答に感謝しますが、likeButtonClickedアクションをテーブルビューで作成すると、私は答えがないので "child(answer.id)"と書くことができません。私は、回答をリストとして保持し、回答として[indexPath.row]という1つの回答をテーブルビュー機能で使用します。これについての助けてください? – atunaysaka

+0

私の更新された回答を確認することができます。私はchanges.Hopeあなたの答えを得ることを期待して更新されたとしてマークしました。上記の解決策に関する問題に直面したら私に知らせてください – Vikky

+0

私は私の答えを得た、 。お返事ありがとうございました! – atunaysaka

関連する問題