下面是我的多项 Select 题测试的简化版本. 比如说,我们有五个问题,每个问题有四个答案选项.
Аll five questions are answered one after another/one at a time, that is one question with the corresponding answer choices is shown on a separate screen. Example below. ![Text]()
在最后一个屏幕上,我想对完成的工作做一个总结,并将所有问题和答案呈现在一个屏幕上.
这个问题可能看起来很愚蠢,但我已经绞尽脑汁想了一段时间了,解决不了它.
问题在于,在最后一个屏幕上,问题本身被正确呈现,但对它们的答案选项却不正确:在四个单元格中的每一个中只重复显示第四个答案选项,而不是像在数据库QuestionManager
中那样重复显示不同的答案选项.下面是一个例子.(第二行显示"Option2"而不是数据库中的数字,因为CustomFinalCell
中的option2.text = answer.text
出于实验目的被注释掉了.)如何让它像数据库中一样显示不同的答案选项?
class QuestionManager {
enum LevelType: String {
case beginners = "Beginners"
case middle = "Middle"
case advanced = "Advanced"
}
// MARK: - Properties
private (set) var questions: [Question] = []
private (set) var answers: [Answer] = []
private (set) var currentQuestion: Question?
private (set) var questionNumber: Int = 0
public var levelType: LevelType = .beginners
// MARK: - Constants
public let defaultQuestions: [LevelType: [Question]] = [
.advanced: [
Question(text: "What is 2 / 2?", answers: [
Answer(text: "1", correct: true),
Answer(text: "2", correct: false),
Answer(text: "4", correct: false),
Answer(text: "7", correct: false)
], explanation: "The correct answer will be is as it is a Singular form."),
Question(text: "What is 10 / 2?", answers: [
Answer(text: "1", correct: false),
Answer(text: "12", correct: false),
Answer(text: "4", correct: false),
Answer(text: "5", correct: true)
], explanation: "The correct answer will be is as it is a Singular form."),
Question(text: "What is 27 / 3?", answers: [
Answer(text: "10", correct: false),
Answer(text: "9", correct: true),
Answer(text: "11", correct: false),
Answer(text: "12", correct: false)
], explanation: "The correct answer will be is as it is a Singular form."),
Question(text: "What is 32 / 4?", answers: [
Answer(text: "7", correct: false),
Answer(text: "8", correct: true),
Answer(text: "9", correct: false),
Answer(text: "10", correct: false)
], explanation: "The correct answer will be is as it is a Singular form."),
Question(text: "What is 100 / 2?", answers: [
Answer(text: "100", correct: false),
Answer(text: "30", correct: false),
Answer(text: "40", correct: false),
Answer(text: "50", correct: true)
], explanation: "The correct answer will be is as it is a Singular form.")
]
]
}
class CustomFinalCell: UITableViewCell {
private var questionManager: QuestionManager!
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var option1: UILabel!
@IBOutlet weak var option2: UILabel!
@IBOutlet weak var option3: UILabel!
@IBOutlet weak var option4: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
// MARK: - Public methods
public func configure(with question: Question, with answers: [Answer]) {
questionLabel.text = question.text
// option1 = viewWithTag(1) as? UILabel
// option2 = viewWithTag(2) as? UILabel
// option3 = viewWithTag(3) as? UILabel
// option4 = viewWithTag(4) as? UILabel
for answer in answers {
option1.text = answer.text
// option2.text = answer.text
option3.text = answer.text
option4.text = answer.text
}
}
}
class ResultViewController: UIViewController {
// MARK: - Properties
private var questionManager: QuestionManager!
private var questions: [Question] = []
private var answers: [Answer] = []
// MARK: - Create
static func create(with questionManager: QuestionManager) -> ResultViewController {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "result") as! ResultViewController
vc.questionManager = questionManager
return vc
}
@IBOutlet weak var tableView: UITableView!
// MARK: - Private methods
private func configureTableView() {
tableView.delegate = self
tableView.dataSource = self
tableView.register(UINib(nibName: "CustomFinalCell", bundle: nil), forCellReuseIdentifier: "customFinalCell")
// tableView.backgroundColor = .gray
// self.tableView.reloadData()
print("Hello, world!")
}
private func configureQuestionView() {
for question in questions {
let questionView = QuestionView(question: question)
tableView.addSubview(questionView)
}
}
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .cyan
navigationItem.setHidesBackButton(true, animated: true)
configureTableView()
}
}
// MARK: UITableViewDelegate, UITableViewDataSource
extension ResultViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
questionManager.questions.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "customFinalCell", for: indexPath) as? CustomFinalCell else {
return UITableViewCell()
}
let question = questionManager.questions[indexPath.row]
answers = question.answers
cell.configure(with: question, with: answers)
return cell
}
}