2017-05-04 61 views
0

我的应用程序中有一个食物日记,应该能够插入包含用户输入食物信息的行。每次我尝试我得到这个错误:''NSInternalInconsistencyException',原因:'尝试插入第3行到第0部分,但更新后的第0部分只有2行'“当尝试在tableView中插入行时出现NSInternalInconsistencyException

这是执行ViewController代码插入。

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,显示用户何时用协议方法输入数据。

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 

这两个VC的照片。

FoodDiary

PopUpVC

我怎样才能获得该行与信息插入用户输入?

+1

'tableData.count + 1'应该是'tableData.count - 1' – dan

+0

不工作。显示如下:''NSInternalInconsistencyException',原因:'无效更新:第1节中的行数无效。更新(2)后现有节中包含的行数必须等于该节前包含的行数更新(1),加上或减去从该部分插入或删除的行数(插入0,删除0),加上或减去移入或移出该部分的行数(移入0,移出0) ''@dan – thelegendary3

回答

1

正如Dan在评论中所说,您需要tableData.count - 1作为您的新索引路径行。例如,如果数组中有两个元素(count = 2),则行0和行1(即count-1是最后一行)。例如,如果数组中有两个元素

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. 
} 
+0

它仍然无法正常工作。现在显示如下:“'NSInternalInconsistencyException',原因:'无效更新:第1节中的行数无效。更新(2)后现有节中包含的行数必须等于该节中包含的行数在更新之前(1),加上或减去从该部分插入或删除的行数(插入0,删除0),加上或减去移入或移出该部分的行数(移入0,移出0 )''“ – thelegendary3

+0

您的部分号码是否正确?这个错误表明你没有插入任何行,但是你做到了。我注意到,作为注释添加到另一个答案的'numberOfRowsInSection'对于不同的部分没有做任何事情。所以你在第0节中插入一个新的行,但是因为你似乎在所有的节中都使用了相同的数组,所以第1节也看到了它没有的新行;期望和你得到一个例外 – Paulw11

+0

我基本上通过我的PrepareforSegueMethod节。像这样:如果segue.identifier ==“diaryEntry”{如果让selectedIndexPath = tableView.indexPathForSelectedRow?.first {0}将popVC = segue.destination设置为! PopupVC popVC.delegate =自 如果selectedIndexPath == 0 { 让利标签= “添加早餐” popVC.foodLabel =标签 popVC.section = 0 我传递的部分我PopUpVC所以它知道什么部分行必须插入。 – thelegendary3

相关问题