1

I've built a likert quiz and I'm trying to create a results page that looks like the second view controller. So far all I've been able to return is the score and personality ranked (first view controller).

I'm not sure how I can show the quiz results in order. I basically would want to rank them and adjust the cells, while showing the point total but I have no clue what to do.

enter image description here

class SurveyResultsViewController: UITableViewController {

    @IBOutlet var lblSortedScores       : [UILabel]!
    @IBOutlet var sortedTitle           : [UILabel]!
    @IBOutlet weak var cellFinishButton : UITableViewCell!

    var survey: LikertSurvey?
    var points = [0, 0, 0, 0]
    var results: [(Int, Int)] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        UserDefaults.standard.setValue(points, forKey: "points")

        self.setResults()
        self.initUI()
    }

    private func setResults() {
        for (index, point) in points.enumerated() {
            results.append((index, point))
        }

        results.sort { (result1, result2) -> Bool in
            return result1.1 > result2.1
        }
    }

    private func initUI() {
        for i in 0 ..< results.count {
            let title = survey!.questionCategories[results[i].0]
            lblSortedScores[i].text = "\(title) = \(results[i].1) points"
            sortedTitle[i].text = "\(title)"
        }

        let finishButtonTap = UITapGestureRecognizer(target: self, action: #selector(self.finishButtonTapped(_:)))
        cellFinishButton.addGestureRecognizer(finishButtonTap)

        self.navigationItem.hidesBackButton = false
    }

    @objc func finishButtonTapped(_ sender: UITapGestureRecognizer) {
        self.survey?.completed = true
        let alertController = UIAlertController(title: "Congratulations! You earned 100 XP from completing this quest!", message: "", preferredStyle: .alert)

                     alertController.addAction(UIAlertAction(title: "Ok",
                                                             style: .default) { action in
                         self.performSegue(withIdentifier: "unwindToSectionTableView", sender: self)
                     })

                     self.present(alertController, animated: true, completion: nil)

    }
}

1 Answers1

0

Try restructuring your code like so

var results: [(index: Int, points: Int)] = []
...

private func setResults() {
    for (index, point) in points.enumerated() {
        results.append((index, point))
    }

    results.sort{ $0.points > $1.points }
    self.initUI()
}
MNG MAN
  • 69
  • 1
  • 4