LgSus97
LgSus97

Reputation: 93

Validate switch for all rows in table view

I have a switch inside table view cell (I have a table view inside table view, in the fist table view I have the label for the question and other table view for the switch with the label for the answer, I load a view from xib), I try to validate when user chose an answer for each of the questions, and validate if the select answer is correct, I have this JSON from de API that tell me if the answer is correct or not

"answers": [
    {
        "id": 1,
        "answer": "si",
        "isCorrect": false,
    },
    {
         "id": 2,
         "answer": "no",
         "isCorrect": true,
    }
]

This is my first view controller where I have my first table view


class QUIZViewController: UIViewController {
  
  @IBOutlet weak var tableView: UITableView!
  @IBOutlet weak var tableViewHC: NSLayoutConstraint!
  @IBOutlet weak var continueButton: UIButton!

  
  var challenge : Challenge = Challenge()
  
  var questionsCount : Int = 0
  
  override func viewDidLoad() {
    super.viewDidLoad()
    setTextAndStyles()
    setTableView()
    tableViewHC.constant = tableView.contentSize.height
  }
  
  override func viewDidLayoutSubviews() {
    self.tableView.layoutIfNeeded()
    tableViewHC.constant = tableView.contentSize.height
    self.tableView.layoutIfNeeded()
  }

  func setTableView(){
    tableView.delegate = self
    tableView.dataSource = self
    
    let nib = UINib(nibName: "QuestionListTableViewCell", bundle: nil)
    tableView.register(nib, forCellReuseIdentifier: "questionListCell")
    
    
    tableView.isScrollEnabled = false

    continueButton.solid()
    continueButton.isEnabled = false
    continueButton.setTitle("CONTINUAR", for: .normal)
  }

  func validations(){
    // Here I have to validate if user chose an answer for each of the questions, after that I set enable the button continue
  }
  
extension QUIZViewController:  UITableViewDelegate, UITableViewDataSource{
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    tableViewHC.constant = tableView.contentSize.height
    return questionsCount
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "questionListCell", for: indexPath) as! QuestionListTableViewCell
    cell.separatorInset.right = cell.separatorInset.left
    cell.questionLabel.text = "Question"
    cell.setup(answers: challenge.questions?[indexPath.row].answers ?? [Answer]())
    tableViewHC.constant = tableView.contentSize.height
    return cell
  }
}

This is second table view class where I have the switch with label for the answer

extension QuestionListTableViewCell: UITableViewDelegate, UITableViewDataSource {
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return answers.count
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "answerListCell", for: indexPath) as! AnswerListTableViewCell
    cell.separatorInset.right = cell.separatorInset.left
    cell.answerList.optionLabel.text = answers[indexPath.row].answer
    cell.answerList.optionSwitch.tag = indexPath.row
    cell.answerList.optionSwitch.addTarget(self, action: #selector(self.switchChanged(_:)), for: .valueChanged)
    cell.answerList.optionSwitch.isOn = indexPath.row == selectedSwitchIndex

    return cell
  }

  @objc func switchChanged(_ sender: UISwitch!){
    guard sender.isOn else {
        return
    }
    selectedSwitchIndex = sender.tag
    print(answers[sender.tag].id)       // this the id for the answer
    print(answers[sender.tag].isCorrect)// this boolean tell me if is correct
    tableView.reloadData()
    
    list.append([answers[sender.tag].id:answers[sender.tag].isCorrect])
  }

}

enter image description here

I think about a solution it could be create a list with the id and boolean then use a callback to validate if user chose an answer for each of the questions and finally check is the answers are correct or not but I am no sure about that, what is the best way to do that ?

Upvotes: 0

Views: 145

Answers (0)

Related Questions