カスタムセル内にお気に入りボタンがあります。 tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
では、お気に入りボタンをクリックするたびにリスナーを設定しました。今、私は2つのセクションを持っています。セクション0はすべて食品、セクション1はお気に入りです。私がendUpdates()に到達すると、アプリはNSInternalInconsistencyException
の例外でクラッシュします。 tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
関数は、addFavorites
の中で決して呼び出されません。 favoriteFoods
が初期化されました。コード内で他の場所にはbeginUpdates
とendUpdates
を呼び出すことはありません。でUITableViewのセクションに行を追加できません
func addFavorites(sender: UIButton) {
let touchPoint = sender.convert(CGPoint(x: 0, y: 0), to: companyTableView)
let indexPath = companyTableView.indexPathForRow(at: touchPoint)
favorites.append(allFood[(indexPath?.row)!])
tableView.beginUpdates()
tableView.insertRows(at: [IndexPath(row: favoriteFoods.count - 1, section: 1)], with: .automatic)
tableView.endUpdates()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
if (section == 0) {
return allFood.count
} else {
return favorites.count
}
}
アサーション障害 - [のUITableView _endCellAnimationsWithContext:]、/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.6.21/UITableView.m:1594
「無効な更新:更新後のテーブルビューに含まれるセクションの数が、(2)に含まれるセクションの数に等しくなければならないセクション1の行の無効数によりキャッチされない例外「NSInternalInconsistencyException」、理由にアプリを終了update(1)の前のテーブルビューで、挿入または削除されたセクションの数をプラスまたはマイナスします(0は挿入され、0は削除されます)。あなたは配列に新しい行を追加している
'numberOfRowsInSection'コードを表示してください。更新された行数を正確に反映していません。 – Paulw11
を編集しました。また、else節にprintステートメントを入れて、お気に入りセクションの行数が1に増加しています。 – stumped
新しい行を'favorites'しかし' numberOfRowsInSection'は 'favoriteFoods' – Paulw11