下面是我的多项 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](enter image description here)

在最后一个屏幕上,我想对完成的工作做一个总结,并将所有问题和答案呈现在一个屏幕上.

这个问题可能看起来很愚蠢,但我已经绞尽脑汁想了一段时间了,解决不了它.

问题在于,在最后一个屏幕上,问题本身被正确呈现,但对它们的答案选项却不正确:在四个单元格中的每一个中只重复显示第四个答案选项,而不是像在数据库QuestionManager中那样重复显示不同的答案选项.下面是一个例子.(第二行显示"Option2"而不是数据库中的数字,因为CustomFinalCell中的option2.text = answer.text出于实验目的被注释掉了.)如何让它像数据库中一样显示不同的答案选项?

![Text](enter image description here)

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
  }
}

推荐答案

您的CustomFinalCell类中的for循环不正确.try 替换这部分代码

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行分配给数组中的最终答案.

Ios相关问答推荐

使用UIGraphicsBeginIMAext创建新图像会扭曲图像 colored颜色

如何修复使用EFCore和SQLite的.Net MAUI iOS应用程序上的当前应用程序不支持NullabilityInfoContext?

通过拖动更改视图位置的SwiftUI会引发UI错误

设置托管在DigitalOcean上的iOS通用链接

UIAlertController的空子类是否可以与`appearance(whenContainedInInstancesOf:)`一起安全使用

由于已存在同名项目,因此无法将Mapbox.xcframework-ios.sign复制到Signature中

如何在expo-react native中从ios平台中排除包

如何在SwiftUI中实现淡入淡出加粘标题屏幕效果

SwiftUI 菜单中的内联水平按钮

如何为 XCTest、SwiftUI 预览等初始化带有 @MainActor 注释的 Swift 类

FileHandle 不会在 iOS 中释放内存

使用 CIImage 支持的 UIImage 设置 UIImageView 时发生罕见的崩溃

如何在 SwiftUI 中检测 TextField 的实时变化?

当键盘出现在 iOS 中时,将 UIView 上移

在 iOS 7 上更改标签栏色调 colored颜色

~= Swift 中的运算符

如何以编程方式切换 UISegmentedControl?

iOS:以编程方式制作屏幕截图的最快、最高效的方法是什么?

如何关闭 iOS 键盘?

在 SKScene 中设置按钮