尝试在 tableView 中插入行时出现 NSInternalInconsistencyException [英] NSInternalInconsistencyException when trying to insert row in tableView

查看:35
本文介绍了尝试在 tableView 中插入行时出现 NSInternalInconsistencyException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个食物日记,它应该能够插入一行,其中包含用户输入的食物信息.每次我尝试时都会收到此错误:'NSInternalInconsistencyException',原因:'尝试将第 3 行插入第 0 部分,但更新后第 0 部分中只有 2 行'"

I have a food diary in my app that should be able to insert a row with information of the food the user has inputed. Everytime I try I get this error: "'NSInternalInconsistencyException', reason: 'attempt to insert row 3 into section 0, but there are only 2 rows in section 0 after the update'"

这是执行插入的 ViewController 代码.

This is the ViewController code that performs the insert.

class FoodDiary: UITableViewController, AddRowDelegate {


var tableData = [[String: Any]]() //Datasource

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

    let getName = ["name":name, "calories":calories] as [String: Any]
    tableData.append(getName)
   let indexPath = IndexPath(row: tableData.count + 1, section: section)
    tableView.insertRows(at: [indexPath], with: .automatic)


    calsforToday.text = calories


    tableView.reloadData()
}

这是我的另一个 ViewController,它显示用户何时使用协议方法输入数据.

This is my other ViewController that shows when the user is inputting the data with the protocol method.

    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) {


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


    dismiss(animated: true, completion: nil)


}
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

两位风投的照片.

如何让行插入用户输入的信息?

How can I get the row to insert with the information the user inputs?

推荐答案

正如 Dan 在评论中所说,您的新索引路径行需要 tableData.count - 1.

As Dan said in a comment, you need tableData.count - 1 for your new index path row.

例如,如果您的数组中有两个元素(count = 2),那么您有第 0 行和第 1 行(即 count-1 是最后一行).

For example, if there are two elements in your array (count = 2) then you have rows 0 and 1 (i.e. count-1 is the last row).

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

    let getName = ["name":name, "calories":calories] as [String: Any]
    tableData.append(getName)
    let indexPath = IndexPath(row: tableData.count-1, section: section)
    tableView.insertRows(at: [indexPath], with: .automatic)

    calsforToday.text = calories
}

其他几点:

  1. 无需重新加载整个表,因为您已经插入了新行.重新加载会产生不需要的视觉效果
  2. 我建议您为数据模型创建一个结构体,而不是使用字典
  3. 每个部分都需要一个数组.

struct FoodItem {
    var name: String
    var calories: String // This should probably be an Int
}

class FoodDiary: UITableViewController, AddRowDelegate {

    var tableData = Array(repeating: [FoodItem](), count: 3) 

    func numberOfSections(in tableView: UITableView) -> Int {
        return self.tableData.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.tableData[section].count
    }

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

        let newItem = FoodItem(name:name, calories:calories)
        tableData[section].append(newItem)
        let indexPath = IndexPath(row: tableData[section].count-1, section: section)
        tableView.insertRows(at: [indexPath], with: .automatic)

        calsforToday.text = calories
    }

  //  Not shown; update your `cellForRow(at:)` to use the indexpath.section and row to get the required array and item. 
}

这篇关于尝试在 tableView 中插入行时出现 NSInternalInconsistencyException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆