Invalid Update: Invalid Number of Rows in Section 0

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];

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
}

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, 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()
}
}
}

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)
}

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.

Xamarin.Forms SignalR: Invalid update: invalid number of rows in section 0

Make sure you are on MainThread when you access an ObservableCollection that is bound to a UI view.

  • Test MainThread.IsMainThread during Debug testing, throw exception if not on that thread (but you thought all calling code was already on that thread).

  • Wrap code in Device.BeginInvokeOnMainThread. Doing so ensures you aren't altering the collection in middle of UI update:

.

Device.BeginInvokeOnMainThread( () => {
.. do something with `Messages` collection ..
});

CAUTION: That BeginInvokeOnMainThread runs independently, so you can't rely on it "finishing" before whatever code comes next.

If this "fire and forget" isn't sufficient/appropriate, then you'll need to use a Task Continuation instead. Or do the manual equivalent - I don't have an example at my fingertips, but the principle is that you run on main thread, then have an Action that you call when that work is done.

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


Related Topics



Leave a reply



Submit