自定义单元格segue与数据传递错误 - Swift

时间:2015-06-11 00:00:10

标签: swift tableview cell segue

我正在创建一个测验应用程序,表格视图向用户显示他们正确/错误的问题。当用户点击一个问题时,它会将他们带到一个显示问题解释的新页面。

澄清:

  • 我已正确标记了我的segue标识符
  • 我通过控制从原型单元格拖动到InterpreViewController来创建两个控制器之间的链接
  • 我为自定义单元格创建了一个新类,而不是在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] 
            }
        }     }
}


}

第二视图控制器

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

2 个答案:

答案 0 :(得分:0)

很可能基于您的代码,您的explanations数组没有索引i的元素。

答案 1 :(得分:0)

您的阵列说明中似乎没有任何数据显示,请尝试使用以下方法进行修复:

-dealloc

但是我建议你使用一个字典数组,你可以有一个问题键和另一个答案,以避免这种不匹配。