【发布时间】:2016-01-15 17:19:30
【问题描述】:
我的应用程序将允许用户创建一个注册表单,它将使用 UITableView 显示。我在更新 UITableView 时遇到问题。只是为了解释一下发生了什么,我有两个场景。第一个场景显示创建的问题,第二个场景允许用户创建一个问题。第 1 个场景转至第 2 个场景,然后第 2 个场景展开转至使用问题数据看到的第 1 个场景。这是我的代码。
第一个场景:
@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)
【问题讨论】:
-
实际上,我通过其他测试方式发现将其声明为类型 Question 是可行的。这不是与更新数组有关的错误的原因,因此我可能会编辑此问题。
-
tableView声明在哪里? -
在第一个场景中。我将使用 tableview 代码编辑我的帖子
-
您发布了 tableview 委托函数,但没有发布
tableView声明的位置。是否有一行看起来像这样:@IBOutlet weak var tableView: UITableView!? -
代码的第一行就是这么说的!
标签: ios arrays swift uitableview