You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS多选测试结果页答案重复显示问题求助

解决iOS测试应用结果页选项重复显示最后一个答案的问题

你遇到的问题核心出在CustomFinalCell的configure方法里的循环逻辑——遍历answers数组时,你每次循环都把当前answer的text赋值给option1、option3、option4,等循环跑完,这几个label自然就只保留最后一个answer的内容了。

修改方案

直接根据answers数组的索引给对应选项label赋值,同时增加安全判断避免数组越界:

class CustomFinalCell: UITableViewCell {
    
    @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()
    }
    
    public func configure(with question: Question, with answers: [Answer]) {
        questionLabel.text = question.text
        
        // 通过索引匹配对应选项,加安全判断防止数组越界
        option1.text = answers.count > 0 ? answers[0].text : ""
        option2.text = answers.count > 1 ? answers[1].text : ""
        option3.text = answers.count > 2 ? answers[2].text : ""
        option4.text = answers.count > 3 ? answers[3].text : ""
    }
}

额外优化

ResultViewController里的answers实例变量属于冗余代码,直接传递question.answers即可简化逻辑:

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]
    cell.configure(with: question, with: question.answers)
    
    return cell
}

内容的提问来源于stack exchange,提问作者Parfen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 19:15:19