使用Delegate插入行时出现NSInternalInconsistencyException错误

时间:2017-04-24 19:25:41

标签: ios swift tableview

enter image description here

我试图在我的"保存到日记"时插入一行。单击按钮。但是,我一直收到这个错误:

"' 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()
}

继承我的PopUpVC,将数据发送到我的食物日记:

  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传递到我的食物日记,然后插入用户输入的食物名称。

1 个答案:

答案 0 :(得分:0)

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

应该是:

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