私のアプリには、ユーザーが入力した食品の情報を含む行を挿入できるフードダイアリーがあります。 「NSInternalInconsistencyException」理由:「行3をセクション0に挿入しようとしましたが、更新後にセクション0に2行しかありません」テーブルビューに行を挿入しようとすると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の写真。
はどのようにして情報をユーザ入力を挿入する行を得ることができますか?
'tableData.count + 1 'は、' tableData.countする必要があります - 1' – dan
は動作しません。 "NSInternalInconsistencyException"、理由:無効な更新:セクション1の行数が無効です。更新(2)後に既存のセクションに含まれる行の数は、そのセクションに含まれる行の数と同じでなければなりませんそのセクション(挿入された0、削除された0)から挿入または削除された行の数をプラスまたはマイナスし、そのセクションに出入りする行の数をプラスまたはマイナスしたもの(0は移動、0は移動) '' @dan – thelegendary3