gpt4 book ai didi

swift - 带有数据传递错误的自定义单元格序列 - Swift

转载 作者:行者123 更新时间:2023-11-30 14:18:49 25 4
gpt4 key购买 nike

我正在创建一个测验应用程序,表格 View 向用户展示他们答对/错的问题。当用户点击问题时,他们会进入一个显示问题解释的新页面。

澄清:

  • 我已正确标记我的 Segue 标识符
  • 我通过将控件从原型(prototype)单元拖动到 ExplanationViewController 来创建两个 Controller 之间的链接
  • 我为自定义单元创建了一个新类,但它不在 xib 文件中

基本上,当我点击单元格时,我收到一个错误)线程 1:EXC_BAD_INSTRUCTION。我在下面的代码中概述了错误发生的位置。

class ResultsSummaryViewController: UIViewController,   UITableViewDataSource, UITableViewDelegate {



@IBOutlet weak var tableView: UITableView!

var questions: [String] = []
var finalResults: [Bool] = []
var explanation: [String] = []

override func viewDidLoad() {
super.viewDidLoad()


println(questions)
println(finalResults)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.questions.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ResultTableViewCell

cell.questionLabel.text = questions[indexPath.row]

return cell
}


let testSegueIdentifier = "explanation"


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

if(segue.identifier == testSegueIdentifier) {
if let destination = segue.destinationViewController as? ExplanationViewController {
if let i = tableView.indexPathForSelectedRow()?.row { <-- ERROR HERE
destination.explanation = explanation[i]
}
} }
}


}

第二个 View Controller

class ExplanationViewController: UIViewController {

@IBOutlet weak var explanationLabel: UILabel!

var explanation = String()



override func viewDidLoad() {
super.viewDidLoad()

explanationLabel.text = explanation

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

在结果摘要之前查看 Controller

var answers: [String] = []
var userAnswers: [Bool] = []
var correctAnswers: [Bool] = []
var finalResultsArray: [String] = []
var answerCount: [Bool] = []
var questionString: [String] = []
var explanationString: [String] = []

class QuizQuestion {

let question: String!
let answer: Bool!
let explanation: String!
var usersAnswer: Bool?
var answerSubmitted: Bool?

init(question: String, answer: Bool, explanation: String) {
self.question = question
self.answer = answer
self.explanation = explanation
}
}

let questions = [

QuizQuestion(question: "Do I like coffee?", answer: true, explanation: "Because it's awesome!"),
QuizQuestion(question: "Is bacon god's gift to mankind?", answer: true, explanation: "Because it's awesome!"),
QuizQuestion(question: "Should I take a nap right now?", answer: true, explanation: "You gotta review some code!"),
]

@IBAction func checkResults(sender: AnyObject) {
self.performSegueWithIdentifier("checkResults", sender: sender)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

if(segue.identifier == "checkResults") {
var checkResults = segue.destinationViewController as! ResultsSummaryViewController

checkResults.questions = questionString
checkResults.finalResults = answerCount
checkResults.explanation = explanationString
}
}

func questionToString() {
for (index, _) in enumerate(questions) {
questionString.append(questions[index].question!)
}
}

func explanationToString() {
for (index, _) in enumerate(questions) {
explanationString.append(questions[index].explanation!)
}
}

最佳答案

您的数组解释中似乎没有任何数据可显示,请尝试使用以下方法修复:

if let i = self.tableView.indexPathForSelectedRow()?.row {
if i <= explanation.count{
destination.explanation = explanation[i] <-- ERROR HERE
} else {
println("No explanationto display")
}
}

不过,我建议您使用一个字典数组,其中一个键用于问题,另一个键用于答案,以避免这种不匹配。

关于swift - 带有数据传递错误的自定义单元格序列 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30769363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com