lisen kaci
lisen kaci

Reputation: 179

How do you save user answers received from a textField on a tableView using Swift

I have made a little quiz where a tableView holds different questions and answers as labels and textFields.

questions and answers on tableView

If the correct answer is entered the tableView cell turns green,the textField Text is set, and the textfield becomes uneditable.

correct answer cell

My problem is that none of these settings save. I need it to remember if the question was answered correctly and change the cell accordingly. I know I can use Core Data or User Defaults to save things but i'm not sure how get the specific cells that were answered correctly to save.

My code below:

TableViewController

import UIKit

class TableViewController: UITableViewController, UITextFieldDelegate {

    let questions = ["what is 3 + 5", "what is the meaning of life","what 
    is the number from the jim carrey movie"]
    let answers = ["8","42","23"]

    var textfieldColor = UIColor(red:0.98, green:0.23, blue:0.42, 
    alpha:1.0)
    var textFieldText = ""
    var isTextFieldEditable = true

    override func viewDidLoad() {
        super.viewDidLoad()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }




    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return answers.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Answers", for: indexPath) as! TableViewCell

        cell.question.text = questions[indexPath.row]

        cell.backgroundColor = textfieldColor
        cell.answerOutlet.text = textFieldText
        cell.isUserInteractionEnabled = isTextFieldEditable

        cell.answerOutlet.delegate = self
        return cell
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool
    {

        print(answers.contains(textField.text!))
        if answers.contains(textField.text!)
        {
            textfieldColor = UIColor.green
            textFieldText = textField.text!
            isTextFieldEditable = false

            let rowNumber = answers.index(of: textField.text!)
            let indexPath = IndexPath(item: rowNumber!, section: 0)
            tableView.reloadRows(at: [indexPath], with: .top)
        }
       return true
    }



}

TableViewCell:

import UIKit

class TableViewCell: UITableViewCell {
    @IBOutlet weak var question: UILabel!
    @IBAction func Answer(_ sender: UITextField)
    {
    }
    @IBOutlet weak var answerOutlet: UITextField!


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

Thank you for your help.

Upvotes: 0

Views: 144

Answers (1)

Enrique
Enrique

Reputation: 303

You can add a Boolean check that changes inside the function textFieldShouldReturn and store the value inside an array and save it, something like this:

correct[rowNumber!] = true
UserDefaults.standard.set(correct, forKey: "Correct")

Then, change the loading of the table in tableView using the array:

if correct[indexPath.row] == false {

        cell.backgroundColor = textfieldColor1
        cell.answerOutlet.text = textFieldText
        cell.isUserInteractionEnabled = isTextFieldEditable
        }

        if correct[indexPath.row] == true {

            cell.backgroundColor = textfieldColor2
            cell.answerOutlet.text = answers[indexPath.row]
            cell.isUserInteractionEnabled = false
        }

and finally, check each time you load the view, inside viewDidLoad to see if there are any results stored in UserDefaults:

if let corretAnswers = UserDefaults.standard.object(forKey: "Correct") {
        correct = corretAnswers as! [Bool]
        }

Upvotes: 0

Related Questions