Reputation: 82
My app will allow users to create a registration form and it will be displayed using a UITableView. I'm having trouble updating the UITableView. Just to explain a bit what is going on, I have 2 scenes. The 1st scenes displays the created questions and the 2nd scene allows the user to create a question. The 1st scene segues to the 2nd, and then the 2nd scene unwind segues to the 1st seen with the question data. Here's my code.
1st scene:
@IBOutlet var tableViewObject: UITableView!
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
// code for creating tableView
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.questionsArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.textLabel?.text = self.questionsArray[indexPath.row].Label
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
@IBAction func segueToView(sender: AnyObject) {
}
@IBAction func cancelToSecondViewController(segue:UIStoryboardSegue) {
}
@IBAction func saveQuestion(segue:UIStoryboardSegue) {
if let CreateQuestion = segue.sourceViewController as? createQuestion {
if (CreateQuestion.flag == 0) {
let textinput = CreateQuestion.newInputQuestion
questionsArray.append(textinput)
}
else {
let multichoice = CreateQuestion.newMultiQuestion
questionsArray.append(multichoice)
}
let indexPath = NSIndexPath(forRow: questionsArray.count-1, inSection: 0)
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
}
2nd scene:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SaveQuestion" {
if (questionType.selectedSegmentIndex == 0) {
newInputQuestion = textInput(placeHolder: inputQuestionHint.text!, Label: inputQuestionTitle.text!, required: required.selectedSegmentIndex)
flag = 0
}
else {
var arrayOfAnswers = [String]()
for (var i = 0; i < numAnswers.selectedSegmentIndex + 1; i++) {
arrayOfAnswers.append(numAnswersArray[i].text!)
}
newMultiQuestion = multiChoice(answers: arrayOfAnswers, Label: multiQuestionTitle.text!, required: required.selectedSegmentIndex)
flag = 1
}
}
}
I get the following error when I try to create a question:
fatal error: unexpectedly found nil while unwrapping an Optional value
It highlights the following line:
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
Upvotes: 0
Views: 216
Reputation: 13316
tableView
is nil
. Since it is an @IBOutlet
that is configured by interface builder, something is wrong with how you configured it in interface builder.
The most likely culprit is that you did not set the class for your view controller in the identity inspector.
Go to interface builder and make sure the utilities pane is open. Open the identity inspector in the utilities pane. Near the top is a place for you to enter the class name of your custom view controller. So if your view controller is called MyViewController
, make sure to enter MyViewController
into that field.
If that doesn't work, then the @IBOutlet
isn't configured properly. Check your connections, and then double check them.
Upvotes: 1