Reputation: 3293
There is a favorites button within my custom cell. In the tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
I set up a listener for whenever the favorites button is clicked. Right now, I have two sections. Section 0 is all foods and section 1 is favorites. When I get to endUpdates(), the app crashes with the NSInternalInconsistencyException
exception. The tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
function is never called within addFavorites
. favoriteFoods
is initialized, and I've never called beginUpdates
and endUpdates
anywhere else in my code.
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
}
}
Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.6.21/UITableView.m:1594
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of sections contained in the table view after the update (2) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'
Upvotes: 0
Views: 552
Reputation: 3293
I needed to add companyTableView.insertSections([1], with: .automatic)
between begin and end updates because there was no pre-existing section.
Upvotes: 1
Reputation: 115051
You are adding the new row to an array called favorites
but numberOfRowsInSection
is returning the count of an array called favoriteFoods
Upvotes: 0