swift 问题的答案选项被不正确地拉出

zaqlnxep  于 2023-05-05  发布在  Swift
关注(0)|答案(1)|浏览(159)

下面是我的多项选择题的简化版本。比如,我们有五个问题,每个问题有四个答案选项。
所有五个问题都是一个接一个/一次回答一个,即一个问题与相应的答案选项显示在单独的屏幕上。示例如下。![文本](x1c 0d1x)
在最后一个屏幕上,我想对已完成的工作进行总结,并在一个屏幕上呈现所有问题和答案。
这个问题似乎很愚蠢,但我苦思冥想了一段时间,还是解不出来。
问题是,在最后一个屏幕上,问题本身被正确地呈现,但对它们的答案选择不是:在四个单元格的每一个中仅重复显示第四个答案选项,而不是如在数据库X1 M0 N1 X中那样显示不同的答案选项。示例如下。(第2行显示“Option 2”而不是数据库中的数字,因为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
  }
}
h6my8fg2

h6my8fg21#

CustomFinalCell类中的for循环不正确。尝试替换这部分代码

for answer in answers {
    option1.text = answer.text
//    option2.text = answer.text
    option3.text = answer.text
    option4.text = answer.text
}

用这个

option1.text = question.answers[0].text
option2.text = question.answers[1].text
option3.text = question.answers[2].text
option4.text = question.answers[3].text

你的问题就解决了
原来不正确的原因是,在这里使用循环迭代问题的4个答案选项中的每一个,然后在最后一次迭代中,将所有4行分配给数组中的最终答案。

相关问题