2016-10-16 13 views
0

セクションの最後のセルを削除しようとするたびにアプリがクラッシュしてしまいました。セクションの最後のセルを削除した後にiOSアプリがクラッシュする

私のセクションでは、10行を持っている場合例は、私は何の問題もなく、それらを削除することができますが、最後の1は、次のエラーがスローされます。

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (1) must be equal to the number of sections contained in the table view before the update (3), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'

私はここの周りに検索し、この問題を解決するにはいくつかの方法を見つけました私はそれらを試してみたが、この問題は修正されず、クラッシュして同じエラーがスローされる。

それは次のように私のコードの関連部品が行く:

override func numberOfSections(in tableView: UITableView) -> Int { 
    if (main_arr.count > 0 && sub_arr.count > 0) { 
     self.numberOfSections = 3 
    } else { 
     self.numberOfSections = 2 
    } 

    return self.numberOfSections 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if (section == 0) { 
     return 1 
    } else if (section == 1 && main_arr.count > 0) { 
     return main_arr.count 
    } else { 
     return sub_arr.count 
    } 

} 

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 
    return true 
} 

// Override to support editing the table view. 
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == .delete { 

     switch indexPath.section { 

     case 1: 
      main_arr.remove(at: indexPath.row) 

     case 2: 
      sub_arr.remove(at: indexPath.row) 

     default: 
      print("default 001") 
     } 

     tableView.deleteRows(at: [indexPath], with: .fade) 

    } else if editingStyle == .insert { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 

EDIT 1

私arrays.countの際に任意の私がそうnumberOfSectionsを担当し、グローバル変数を処理しようとしました== 0は1だけ減少しますが、問題は解決しませんでした。

私は3つのセクションを持っていて、そのうちの1つのコンテンツ全体を削除したようなエラーメッセージを完全に理解していますが、1つのセクションを削除してデータソースからも削除する必要があります。

答えて

2

numberOfSectionsは、行を削除する前と後で異なる値を返しますが、の部分は削除しません。。ですから、numberOfSectionsで一定の値を返すか、deleteRows

に加えdeleteSectionsを呼び出す必要がありますいずれか覚えておくべき主なものは次のとおりです。

のUITableViewは常にあなたのデータソースとして行とセクションの同じ番号が含まれている必要があります。

numberOfSectionsまたはnumberOfRows dataSourceメソッドで新しい値を返すことはできません。すべての変更は、削除/挿入行(セクション)メソッドで補う必要があります。逆の場合:tableViewを削除/挿入すると、がdataSourceメソッドの対応する値を返す必要があります。あなたの例外メッセージが示すように:

The number of sections contained in the table view after the update (1) must be equal to the number of sections contained in the table view before the update (3), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).

3 + 0≠1です。この場合、クラッシュを避けるために2つのセクションを削除する必要があります。

+0

私よりも速いです。 :) – Eiko

+0

したがって、 'tableView.deleteRows(at:[indexPath]、with:.fade)'の後に 'tableView.deleteSections(sections:IndexSet、with:UITableViewRowAnimation)'を呼び出すべきだと言っています最後の行? –

+0

はい。あるセクションに新しいセルが追加された場合は、そのセルを削除した後にそのセクションに新しいセクションを挿入する必要があります。しかし、私はむしろ3つのセクションを持つことをお勧めしますが、そのうちのいくつかは空であるかもしれません。セクションや行の代わりに行を操作するので、削除/挿入を制御する方が少し簡単です。 – alexburtnik

1

エラーメッセージは実際に非常に役に立ちます。 セクションについては、変更しないことを期待していますが、操作後に異なる値が検出されました。

基本的に、UIKitはコードを呼び出す前にセクションと行の数をチェックし、コードを実行して、新しい数値が操作に一致するかどうかを確認しました。あなたのケースでは、deleteRows(指定されたセクションの行数を減らす必要があります)を呼び出すため、numberOfSectionsデリゲートが別の結果を返します。したがって、deleteSectionsに電話するか、sectionCountを変更しないでください。

ところで、セクションの行数として0を返すことは禁止されていません。

さらに、インスタンス変数にセクション数を格納するのは良いことではないと思います。それは二重の簿記のためになります。

+0

ok。私は3つの一定のセクションでそれを試し、私はそれから取ることができるものを参照してください、私はあなたにできるだけ早くフィードバックします。 –

関連する問題