// // ViewAnswerViewController.swift // Learn Genie // // Created by Suraj Kumar Mandal on 20/09/21. // import UIKit class ViewAnswerViewController: UIViewController { @IBOutlet var viewAnswerHeadLabel: UILabel! @IBOutlet var viewAnswerTableView: UITableView! @IBOutlet var closeButton: UIButton! var selectedAnswer = [SelectedAnswerModel]() let questionDB = DBManager.sharedInstance.database.objects(QuestionModel.self) let choiceDB = DBManager.sharedInstance.database.objects(ChoiceModel.self) override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. viewAnswerTableView.delegate = self viewAnswerTableView.dataSource = self viewAnswerTableView.tableFooterView = UIView() setupUI() for item in selectedAnswer { print("Saved QuesId: \(item.quesId), Saved ChoiceId: \(item.choiceId), Saved Answer: \(item.answerId)") } } func setupUI() { viewAnswerHeadLabel.text = Helper.translateText(inputText: "View Answer") closeButton.setTitle(Helper.translateText(inputText: Constant.Close), for: .normal) } func getQuestion(id:Int) -> String { var question = String() for ques in questionDB { if ques.id == id { question = ques.content } } return question } func getChoiceArray(quesId:Int) -> [Int] { var choiceArray = [Int]() if let row = questionDB.firstIndex(where: {$0.id == quesId}) { for choice in questionDB[row].choices { let id = choice.id choiceArray.append(id) } } return choiceArray } func getChoiceName(id:Int) -> String { var choiceName = String() for choice in choiceDB { if choice.id == id { choiceName = choice.content } } return choiceName } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. } */ @IBAction func closeAction(_ sender: Any) { //self.navigationController?.popViewController(animated: true) self.dismiss(animated: true) } } extension ViewAnswerViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return selectedAnswer.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCell(withIdentifier: "ViewAnswerTableViewCell", for: indexPath) as? ViewAnswerTableViewCell else { return UITableViewCell() } cell.questionLabel.text = Helper.translateText(inputText: getQuestion(id: selectedAnswer[indexPath.row].quesId)) cell.quesId = selectedAnswer[indexPath.row].quesId cell.selectedChoice = selectedAnswer[indexPath.row].choiceId cell.answer = selectedAnswer[indexPath.row].answerId let dataArray = self.getChoiceArray(quesId: selectedAnswer[indexPath.row].quesId) cell.getChoiceArray(choiceArray: dataArray) //cell.answerCollectionView.reloadData() return cell } }