如何根据测验结果排序单元格?

问题描述 投票:1回答:1

我建立了一个Likert测验,我试图创建一个看起来像第二个视图控制器的结果页面。到目前为止,我所能返回的只是得分和个性排名(第一视图控制器)。

我不确定如何显示测验结果。我基本上想对它们进行排名并调整单元格,同时显示总点数,但是我不知道该怎么做。

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)

    }
}
ios swift xcode tableview
1个回答
0
投票

尝试像这样重组代码

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()
}
© www.soinside.com 2019 - 2024. All rights reserved.