NSInternalInconsistencyException錯誤使用委派
我試圖點擊我的「保存到日記」按鈕,當插入行插入行的時候。不過,我不斷收到此錯誤:
「‘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,並插入用戶輸入的食物名稱作爲一行。
在你的資料表陣列將數據追加之前,你有1個值意味着一個row.When你叫你追加一個更有價值的委託是指兩行now.So你有兩個行可用。您有兩行試圖添加一個更多的部分,然後您需要將數據追加到數組中。 –
我看到,表格數據有1行,因爲在這個特定的行中,我設計它說「添加早餐」。我如何在它之前添加另一行,並讓它只說出食物的名稱? @TusharSharma – thelegendary3
爲什麼不只是重新加載表agter附加數據。而不是addRowmethod。 –