2017-05-04 4 views
0

私のアプリには、ユーザーが入力した食品の情報を含む行を挿入できるフードダイアリーがあります。 「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の写真。

FoodDiary

PopUpVC

はどのようにして情報をユーザ入力を挿入する行を得ることができますか?

+1

'tableData.count + 1 'は、' tableData.countする必要があります - 1' – dan

+0

は動作しません。 "NSInternalInconsistencyException"、理由:無効な更新:セクション1の行数が無効です。更新(2)後に既存のセクションに含まれる行の数は、そのセクションに含まれる行の数と同じでなければなりませんそのセクション(挿入された0、削除された0)から挿入または削除された行の数をプラスまたはマイナスし、そのセクションに出入りする行の数をプラスまたはマイナスしたもの(0は移動、0は移動) '' @dan – thelegendary3

答えて

1

Danがコメントで述べたように、新しいインデックスパス行にはtableData.count - 1が必要です。

たとえば、配列に2つの要素(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)後に既存のセクションに含まれる行の数は、そのセクションに含まれる行の数と等しくなければなりません(0が挿入され、0が削除されます)、そのセクションの内外に移動された行の数がプラスまたはマイナスになります(0は移動、0は移動) )。 " – thelegendary3

+0

セクション番号は正しいですか?このエラーは、行を挿入しなかったが、実行したことを示します。私は、あなたが他の答えにコメントとして追加した 'numberOfRowsInSection'が、異なるセクションについて何もしなかったことに気付きました。したがって、セクション0に新しい行を挿入していますが、すべてのセクションで同じ配列を使用しているように見えるため、セクション1では新しい行が表示されます。期待して例外を得る – Paulw11

+0

私は基本的に私のPrepareforSegueMethodを通してセクションを取得しています。同様に:if segue.identifier == "diaryEntry" { if selectedIndexPath = tableView.indexPathForSelectedRow?first { let popVC = segue.destination as! PopupVC popVC.delegate =自己 それは何を知っているので、私は私のPopUpVCにセクションを渡しているselectedIndexPath == 0は{ LETラベル= "朝食を追加" popVC.foodLabel =ラベル popVC.section = 0 場合セクションを挿入する必要があります。 – thelegendary3

関連する問題