1

Firebase에서 Swift로 질문 & Answer 프로그램을 구현하고 있습니다. 테이블 CellCell에 같은 버튼이 있어야합니다. 그러나 게시물의 데이터가 tableView 클래스에 있고 tableViewCell 클래스의 like 단추를 변경할 수 있기 때문에 문제가 발생합니다. 이 둘 사이에 데이터 전송이 필요합니다. 아무도 이것으로 나를 도울 수 있습니까?신속한 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

배치되는 경우이

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 } 

같은 모양하고있는 tableview 클래스에 아래의 코드를 작성합니다? –

+0

AnswerCell에 있습니다. 그것은 테이블 뷰 컨트롤러에 있어야합니까? 귀하의 회신에 감사드립니다. – atunaysaka

답변

0

업데이트] 이것은 당신이 당신은 응답 객체를받을 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 클래스에서 대리인을 만들고 해당 테이블 뷰 코드를 작성한 컨트롤러에 확인하십시오.

2.When 버튼을 클릭하면 위임자가 발생하고 mainViewController에서 콜백을받습니다.

두 번째 방법은 AnswerCell 클래스의 likeButtonClicked에 대한 IBOulet 1.Make이

을 달성했다.

2. IBAction을하지 마십시오.

3.Your 코드는 방법`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 작업을 만들 때 답변이 없으므로 "자식 (answer.id)"을 쓸 수 없습니다. 나는 응답리스트를 유지하고 테이블 뷰 기능에서 대답 [indexPath.row]으로 하나의 답변에 액세스합니다. 이것에 관한 어떤 도움이라도? – atunaysaka

+0

당신은 내 업데이트 된 답변을 확인할 수 있습니다. 내가 changes.Hope 당신의 대답을 얻을 것입니다 업데이트 된 것으로 표시했습니다. 위의 솔루션에 관한 모든 문제에 직면하면 알려주십시오. – Vikky

+0

내가 내 대답을 가지고, 이것이 내가 원하는 정확한 솔루션입니다 . 답장을 보내 주셔서 감사합니다. – atunaysaka