我的应用程序将允许用户创建注册表单,它将使用UITableView显示。我在更新UITableView时遇到问题。只是为了解释一下发生了什么,我有2个场景。第一个场景显示创建的问题,第二个场景允许用户创建问题。第一个场景向第二个场景移动,然后第二个场景展开到第一个用问题数据看到的场景。这是我的代码。
第一幕:
@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)
}
}
第二幕:
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
}
}
}
当我尝试创建问题时出现以下错误:
致命错误:在解包可选值时意外发现nil
它突出显示以下一行:
tableView.insertRowsAtIndexPaths([indexPath],withRowAnimation:.Automatic)
答案 0 :(得分:1)
tableView
是nil
。由于它是由接口构建器配置的@IBOutlet
,因此在接口构建器中配置它的方式有问题。
最可能的罪魁祸首是您没有在身份检查器中为视图控制器设置类。
转到界面构建器并确保实用程序窗格已打开。在实用程序窗格中打开身份检查器。在顶部附近是您输入自定义视图控制器的类名称的位置。因此,如果您的视图控制器被调用MyViewController
,请确保在该字段中输入MyViewController
。
如果这不起作用,则@IBOutlet
配置不正确。检查您的连接,然后仔细检查它们。