Invalid update: invalid number of rows in section 0
You need to remove the object from your data array before you call deleteRowsAtIndexPaths:withRowAnimation:
. So, your code should look like this:
// Editing of rows is enabled
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//when delete is tapped
[currentCart removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
You can also simplify your code a little by using the array creation shortcut @[]
:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
Invalid update: invalid number of rows in section 1
It turns out that rows that are not visible can be deleted.
Initially, I had thought that I would get an error if I tried to delete a row that was not visible because that cell for that row would no longer be in the view.
//if index is not visible:
// - update Data Source
// - DELETE the previous row
// - insert the new row
self?.currentUserChats.remove(at: indexRow)
self?.currentUserChats.insert(chatChanged, at: 0)
self?.tableView.beginUpdates()
self?.tableView.deleteRows(at: [indexPathOfOldChat], with: .none)
self?.tableView.insertRows(at: [newIndex], with: .fade)
self?.tableView.endUpdates()
return
iOS: Invalid update: invalid number of rows in section n
The problem is that you should insert the item in the array first:
itemStore.allItems.append(newItem)
Also, there is a difference between sections and rows in numberOfRowsInSection
(return number of rows for every section) you have a switch that returns the same number, it should be
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemStore.allItems.count
}
Edit :
The problem is when the table loads there is 0 rows for all the sections ( itemStore.allItems.count
is zero ), when you try to insert a row say at section 0 , row 0 -- the dataSource must be updated only for that section , which is not happen in your case as it's the same array that is returned for all sections , so you must either have an array of array where inner array represent number of rows so addition/deletion from it doesn't affect other ones ,,,,, or lock the insert to say section 0 like this
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (section == 0 ) {
return itemStore.allItems.count
}
return 0
}
in this edit i inserted in 2 sections 0 and 2 with no crash because i handled numberOfRowsInSection
to return old numbers for old section that why to be able to insert in all sections you must have a different data source array or manage from numberOfRowsInSection
, see edited demo here Homepwner
Instead of setting footer in viewDidLoad implement this method
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
if(section == 5 ) {
let textLabel = UILabel()
textLabel.text = "No more Item"
return textLabel
}
return nil
}
How can I fix this Invalid update: invalid number of rows in section 0 error?
Your loop for creating the array of index paths is incorrect. You want it to iterate over data.count
, not items.count
. And you want the new index paths to be based on the previous items.count
.
private func addMoreRows(_ data: [Int]) {
var indexPaths = [IndexPath]()
for i in data.indices() {
indexPaths.append(IndexPath(row: items.count + i, section: 0))
}
self.items.append(contentsOf: data)
tableView.insertRows(at: indexPaths, with: .left)
}
Invalid update: invalid number of rows in section 0 with NSFetchedResultsController
Doc states:
controller(_:didChange:at:for:newIndexPath:)
indexPath
The index path of the changed object (this value is nil for
insertions).
So, you should change the code like this:
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .update:
if let indexPath = indexPath {
tableView.reloadRows(at: [indexPath], with: .none)
}
case .move:
if let indexPath = indexPath {
tableView.moveRow(at: indexPath, to: newIndexPath)
}
case .delete:
if let indexPath = indexPath {
tableView.deleteRows(at: [indexPath], with: .none)
tableView.reloadData() // << May be needed ?
}
case .insert:
if let newIndexPath = newIndexPath {
tableView.insertRows(at: [newIndexPath], with: .none)
tableView.reloadData() // << May be needed ?
}
default:
tableView.reloadData()
}
}
}
Error 'Invalid update: invalid number of rows in section 0' attempting to delete row in table
The number of rows in your table is [[ToDoItemSvc retrieveAllToDoItems] count]
. When you delete 1
row in your table, then the number of rows in your table should be 1
less than the number of rows before deleting any rows. After you delete 1
row and call [self.tableView reloadData]
the tableView checks to see how many rows there are in the table. At this point, numberOfRowsInSection
will return [[ToDoItemSvc retrieveAllToDoItems] count]
. This should now be 1
less than it was before you deleted a row.
The short answer is, you need to first remove an item from your dataSource, which appears to be [ToDoItemSvc retrieveAllToDoItems]
then delete a row.
The compliment to this is when you add a row, you need to add an item to your dataSource as well.
These changes need to happen before you call reloadData
.
Edit
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// Actually remove the data from the source
[ToDoItemSvc deleteToDoItem:[ToDoItemSvc retrieveAllToDoItems][indexPath.row]]
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView reloadData];
}
ELI5: A teacher has five students: Alice, Bob, Charlie, Diane, and Eric. Bob's mom picks him up early from school before lunch. After lunch, the teacher takes attendance and panics because he only has four kids when the list says there should be five. Where's Bob?!
If Bob's mom had removed his name from the list when she took him out of school then the teacher wouldn't have panicked.
Related Topics
How to Prevent SQL Injections with User-Search-Terms in Vapor 4 (Fluent 4)
How to Disable a Button of a Nstoolbar of Macos X in Swift
Swiftui - How to Change The Button's Image on Click
Swift 3 Custom Extension of Ns Measurement? Ex. Sheeps to Goats
Swift Flatmap on Array with Elements Are Optional Has Different Behavior
Applying Impulses in Spritekit
Popping Noise Between Audioqueuebuffers
Could Not Find Member <Method Name> for Struct Type in Swift
Cannot Convert Value of Type 'Foo!' to Expected Argument Type 'Foo!'
How to Set Default Clouse Param in View Method
Swift How to Make List Start at The Top with No Padding
Rlmexception', Reason: 'Attempting to Modify Object Outside of a Write Transaction
Calling Consecutive Animations in Swift with Completion Handler
Convert Input Data to Integer in Swift
Need Clarification on Typealias Syntax in Swift
Do Swift Hashable Protocol Hash Functions Need to Return Unique Values
Using Cfarraygetvalueatindex in Swift with Unsafepointer (Aupreset)