gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:30:40 25 4
gpt4 key购买 nike

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 传递到我的食物日记,并插入用户输入的食物名称作为一行。

最佳答案

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

应该是:

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

关于ios - 使用 Delegate 插入行时出现 NSInternalInconsistencyException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43596218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com