I have a tableview which is part of a navigation controller that returns a list of questions.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for:indexPath) as! AnswerTableViewCell
let data = quesList[indexPath.row]
cell.labelName.text = data.sender_name;
cell.labelQuestion.text = data.text;
return cell
}
func fetchQues(){
Ref.queryOrdered(byChild: currentuser).queryEqual(toValue: nil).observe(DataEventType.value, with:{(snapshot)in
if snapshot.childrenCount>0{
self.quesList.removeAll()
for data in snapshot.children.allObjects as![DataSnapshot]{
let dataObject = data.value as? [String: AnyObject]
let userid = dataObject?["sender_id"];
let username = dataObject?["sender_name"];
let questext = dataObject?["text"];
let questionid = dataObject?["id"];
let data = Question(id: questionid as! String, sender_id: userid as! String, sender_name: username as! String, text: questext as! String)
self.quesList.insert(data, at :0)
}
self.AnswerTable.reloadData()
}
})
}
I then have a didselectrow function to segue to a VC part of this navigation stack that lets the user input their answer for the question.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let data = quesList[indexPath.row]
questionselect1 = data.text;
questionid1 = data.id;
performSegue(withIdentifier: self.questSegue, sender: nil)
}
After the user submit the answer to the question, it adds a dictionary into the question dictionary and returns to the list of question screen. The queryorder(bychild:currentuser).queryeuqual(tovalue:nil) would only return questions that have not been answered by the current user. So everytime a user answers a question, that question would get removed from the list.
@IBAction func submitAnswer(_ sender: Any) {
if questiontext.text != "" {
DBProvider.Instance.postAnswers(senderID: userid, senderName: username2, text: questiontext.text!)
DBProvider.Instance.postAnswered()
self.questiontext.text = "";
self.navigationController!.popViewController(animated: true)
//append data to the current question
} else {
alertTheUser(title: "Please Submit A Valid Answer", message: "Question Field Cannot Be Empty");
}
}
This works fine at the moment, however, when there is only one question on the list, and the user answers it, and returns to the questions tableview, the last question does not get removed from the list, which it should be since it has been answered.
I have been trying to figure out why this is happening by changing the segueing method and reloading the view with viewwillload or viewdidload and it does not work.
Hopefully someone understands what I am saying and can provide a answer to my bug.