Custom cell segue with data pass error - Swift

111 views Asked by At

I am creating a quiz app and the table view presents the user with the questions they got right / wrong. When the user taps on a question it takes them to a new page that displys the explanation of the question.

For clarification:

  • I have correctly labeled my segue identifier
  • I have created the link between the two controllers by control dragging from my prototype cell to the ExplanationViewController
  • I have created a new class for my custom cell and its not in a xib file

Basically I am getting an error when I tap the cell ) Thread 1: EXC_BAD_INSTRUCTION. I have outlined where the error occurs in my code below.

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


}

2nd 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.
}


}

View Controller before Results Summary

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

There are 2 answers

6
Icaro On BEST ANSWER

It seems that you don't have any data to display in your array explanation, try to fix using this:

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

However I would suggest you to use a array of dictionary where you can have a key for question and another for answer to avoid this kind of mismatch.

1
Patrick Lynch On

Most likely based on your code is that your explanations array does not have an element for index i.