Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSINTERNALINCONCONSTRICENCEPTION使用委托插入行时出错_Ios_Swift_Tableview - Fatal编程技术网

Ios NSINTERNALINCONCONSTRICENCEPTION使用委托插入行时出错

Ios NSINTERNALINCONCONSTRICENCEPTION使用委托插入行时出错,ios,swift,tableview,Ios,Swift,Tableview,单击“保存到日记”按钮时,我正试图插入一行。然而,我不断地发现这个错误: “'NSInternalInconsistencyException',原因:'尝试将第3行插入节0,但更新后节0中只有2行'” 这是我的密码: class FoodDiary: UITableViewController, AddRowDelegate { var tableData = [""] let foodTimes = ["Add Breakfast","Add Lunch","Add Dinner","

单击“保存到日记”按钮时,我正试图插入一行。然而,我不断地发现这个错误:

“'NSInternalInconsistencyException',原因:'尝试将第3行插入节0,但更新后节0中只有2行'”

这是我的密码:

 class FoodDiary: UITableViewController, AddRowDelegate {


var tableData = [""]
let foodTimes = ["Add Breakfast","Add Lunch","Add Dinner","Add Snacks"]

override func numberOfSections(in tableView: UITableView) -> Int {
    return 4
}

   override func tableView(_ tableView: UITableView, 
  numberOfRowsInSection section: Int) -> Int {

    return self.tableData.count
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "diaryEntry" {
        if let selectedIndexPath = tableView.indexPathForSelectedRow?.first{
        let popVC = segue.destination as! PopupVC
            popVC.delegate = self

            if selectedIndexPath == 0 {
                let label = "Add Breakfast"
                popVC.foodLabel = label
                popVC.section = 0

                }
            if selectedIndexPath == 1{
                let label = "Add Lunch"
                popVC.foodLabel = label
                popVC.section = 1

            }
            if selectedIndexPath == 2 {
                let label = "Add Dinner"
                popVC.foodLabel = label
                popVC.section = 2

            }
            if selectedIndexPath == 3{
                let label = "Add Snacks"
                popVC.foodLabel = label
                popVC.section = 3

            }

        }
    }
  }


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FoodDiaryCell

    cell.foodLabel.text = foodTimes[indexPath.section]

return cell
    }

func didAddRow(name: String, calories: String, section: Int) {

    tableData.append(name)
    let rowIndexPath = IndexPath(row: tableData.count + 1, section: section)

    tableView.beginUpdates()
    tableView.insertRows(at: [rowIndexPath], with: .automatic)
    tableView.endUpdates()
}
这是我的POUPVC,它将数据发送到我的食物日记:

  protocol AddRowDelegate {
  func didAddRow(name : String, calories : String, section : Int)
 }     


 class PopupVC: UIViewController {

var delegate: AddRowDelegate?
var section: Int?

@IBOutlet weak var foodTimeLabel: UILabel!
@IBOutlet weak var foodPopup2: UIView!
@IBOutlet weak var foodPopUp: UIView!
@IBOutlet weak var inputFood: UITextField!
@IBOutlet weak var inputCals: UITextField!

@IBAction func saveToDiary(_ sender: Any) {

    dismiss(animated: true, completion: nil)


    delegate?.didAddRow(name: inputFood.text!, calories: inputCals.text!, section: section!)

}
我基本上想做的是将PopUpVC中的信息传递到我的食物日记中,并插入用户输入的食物名称作为一行

let rowIndexPath = IndexPath(row: tableData.count + 1, section: section)
应该是:

let rowIndexPath = IndexPath(row: tableData.count - 1, section: section)

在将数据追加到tabledata数组中之前,您有1个值表示一行。当您调用委托时,您追加了一个值表示现在有2行。因此,您有两行可用。您有一个包含两行的节试图再添加一行,然后您需要在数组中追加数据。我明白了,表格数据有一行,因为在这一行中,我将其设计为“添加早餐”。如何在它前面添加另一行,让它只说出食物名称@Tusharsharmawa为什么不在添加数据后重新加载表agter。而不是addRowmethod。我正在使用addRow方法从PopUpVC中获取数据,并使用委托方法将其传递给我的FoodDiaryVC。这不是正确的方法吗@TusharSharmaI上传了一张图片,看看它是否有助于澄清我所说的内容@TusharSharmaI立即获取此错误:“无效更新:第1节中的行数无效。更新后(2)现有节中包含的行数必须等于更新前(1)该节中包含的行数。”,加上或减去从该部分插入或删除的行数(0插入,0删除)和加上或减去移入或移出该部分的行数(0移入,0移出)“我的应用程序中有一个默认行,告诉用户“添加早餐”。让我知道我是否应该上传一个picA默认行?我上传了一个pic。基本上,这些行告诉用户在特定时间内添加特定食物@GaetanZTry tableData.count,而不是tableData.count-1。但我在你的代码中看不到这一默认行。我基本上希望点击“添加早餐”,然后键入我吃的食物,然后保存该行,并快速更新tableview。这有意义吗?