我正在创建一个测验应用程序,表格视图向用户显示他们正确/错误的问题。当用户点击一个问题时,它会将他们带到一个显示问题解释的新页面。
澄清:
基本上我点击单元格时出错了。线程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]
}
} }
}
}
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.
}
}
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!)
}
}
答案 0 :(得分:0)
很可能基于您的代码,您的explanations
数组没有索引i
的元素。
答案 1 :(得分:0)
您的阵列说明中似乎没有任何数据显示,请尝试使用以下方法进行修复:
-dealloc
但是我建议你使用一个字典数组,你可以有一个问题键和另一个答案,以避免这种不匹配。