Posts

Post not yet marked as solved
4 Replies
1.5k Views
I am creating a quiz app where each question in the quiz is presented by a tableview embedded in a navigationcontroller. I have a score label set in the right bar button item of the navigation controller that I want to update each time a user chooses an answer (tableviewcells). I can't seem to get the score label to update with my code.. Any thoughts?Btw there is more stuff in my viewDidLoad but i just kept the relevant stuff in for the code snippet.var score: Int = 0 { didSet { navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Score: \(score)", style: .plain, target: nil, action: Selector(("updateScore"))) } } override func viewDidLoad() { super.viewDidLoad() score = 0 } @objc func updateScore() { var newScore = score for question in questionsList { if question.selectedAnswerIndex == question.correctAnswerIndex { newScore += 1 } } score = newScore }Also the selectedAnswerIndex and correctAnswerIndex comes from my question model class that contains the quiz questions in my questionsList array. Is this possible to do with a bar button item or should I be looking into other ways to represent the score..?
Posted
by JLago.
Last updated
.
Post not yet marked as solved
4 Replies
940 Views
Kind of an odd question but I've been making a quiz app and am storing the quiz data in Firebase Firestore. I can not seem to figure out how to load the quiz data into my static table view. The quiz question table view controller consists of two static table view sections, the first being one cell for the actual question, and the second being 4 cells for each answer choice. All the tutorials for Firestore seem to be using dynamic table views to load their data in.. Has anyone who worked with Firebase Firestore worked around this or has advice? Thanks.Code for reference:import UIKit import Firebase class QuestionTableViewController: UITableViewController { @IBOutlet weak var questionCell: UITableViewCell! @IBOutlet weak var optionCellA: UITableViewCell! @IBOutlet weak var optionCellB: UITableViewCell! @IBOutlet weak var optionCellC: UITableViewCell! @IBOutlet weak var optionCellD: UITableViewCell! var questionArray = [Question]() var db: Firestore! override func viewDidLoad() { super.viewDidLoad() navigationItem.title = "Question" navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil) tableView.sectionHeaderHeight = 50 tableView.tableFooterView = UIView() db = Firestore.firestore() loadData() } func loadData() { db.collection("QuestionBank").getDocuments() { querySnapshot, error in if let error = error { print("\(error.localizedDescription)") } else { self.questionArray = querySnapshot!.documents.compactMap({Question(dictionary: $0.data())}) DispatchQueue.main.async { self.tableView.reloadData() } } } } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { return 2 } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let controller = ResultsViewController() navigationController?.pushViewController(controller, animated: true) } }
Posted
by JLago.
Last updated
.
Post not yet marked as solved
6 Replies
1.1k Views
Hi, I am making an app that is supposed to test the user's memory by making them watch a video and then giving them a 15 question quiz on that video. The app is supposed to only be used once a day (the user should only watch one video and answer its questions per day). That's a lot of quiz data to handle... what are your guys' recommendationg on going about this? I've heard put the quiz questions and answers in a JSON file and then decode that into my app (requires a web service to hold the json file). I've also heard about realm database and MySQLite that might be handy for my situation. One thing I should note is that 3 of the 15 video quiz questions should be recurring from the previous days' video questions (to test the user's memory). Would this feature alone make it necessary to use a database? I've never worked with databases before but people have said realm is easy to learn. Do you guys think I should go the json route or bite the bullet and use a database for my app?
Posted
by JLago.
Last updated
.