Question

Whenever I hit the DeleteRows code, I get an exception telling me that the number of rows before and after the update need to be the same. Here's the official text:

Reason: Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).

My code is:

        public override void CommitEditingStyle (UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath)
    {
        if (editingStyle == UITableViewCellEditingStyle.Delete)
        {
            tableView.DeleteRows(new [] { indexPath }, UITableViewRowAnimation.Fade);
    // Remove the step from the set of calculations
    _calculation.Steps.RemoveAt(indexPath.Row);
        }
    }
Was it helpful?

Solution

You probably need to change the number returned in

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section

for the indexPath.section to be one lower than before the deletion.

As answered here: Delete row from uitableview crashes

OTHER TIPS

What I found that worked for me was to remove tableView.DeleteRows(new [] { indexPath }, UITableViewRowAnimation.Fade); Your method should look something like this.

public override void CommitEditingStyle (UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath)
    {
        if (editingStyle == UITableViewCellEditingStyle.Delete)
        {
          // Remove the step from the set of calculations
           _calculation.Steps.RemoveAt(indexPath.Row);
           tableView.reloadData();
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top