2017-04-24 32 views
0

enter image description hereNSInternalInconsistencyException錯誤使用委派

我試圖點擊我的「保存到日記」按鈕,當插入行插入行的時候。不過,我不斷收到此錯誤:

「‘NSInternalInconsistencyException’,理由是:‘試圖插入一行3成段0,但只有2排在第0更新後’」

繼承人我的代碼:

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

} 

什麼IM基本上是試圖做的是通過從PopUpVC的信息到我的食物亞爾y,並插入用戶輸入的食物名稱作爲一行。

+0

在你的資料表陣列將數據追加之前,你有1個值意味着一個row.When你叫你追加一個更有價值的委託是指兩行now.So你有兩個行可用。您有兩行試圖添加一個更多的部分,然後您需要將數據追加到數組中。 –

+0

我看到,表格數據有1行,因爲在這個特定的行中,我設計它說「添加早餐」。我如何在它之前添加另一行,並讓它只說出食物的名稱? @TusharSharma – thelegendary3

+0

爲什麼不只是重新加載表agter附加數據。而不是addRowmethod。 –

回答

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

應該是:

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

現在我得到這個錯誤:「無效更新:第1部分中的行數無效。更新(2)後現有部分中包含的行數必須等於該部分之前包含的行數更新(1),加上或減去從該部分插入或刪除的行數(0插入,0刪除),加上或減去移入或移出該部分的行數(移入0,移出0)「我的應用程序中有一個默認行,告訴用戶「添加早餐」。讓我知道我是否應該上傳圖片 – thelegendary3

+0

默認行? –

+0

我上傳了一張照片。基本上這些行告訴用戶在特定時間添加特定食物@ GaetanZ – thelegendary3

相關問題