NSInternalInconsistencyException error in UITableViewDelegate for deleting a cell / Xamarin.iOS

dash_alirezadash_alireza ✭✭USMember ✭✭

I'd like to have two actions in swipe normal (it means right to left), in a table view in Xamarin.iOS.

As I searched, I know that I can customize a UITableViewDelegate for my table and have a swipe with different actions.

Here is my UITableViewDelegate code :

public class NotificationTableDelegate : UITableViewDelegate
{
private readonly AlertsViewController _AlertsViewController;
private List _notifications;

public NotificationTableDelegate(AlertsViewController tagViewController, List<NotificationItem> notifs)
{
    _AlertsViewController = tagViewController;
    _notifications = notifs;
}

public override UITableViewRowAction[] EditActionsForRow(UITableView tableView, NSIndexPath indexPath)
{
    UITableViewRowAction activateOrDeactivateNotifAction = UITableViewRowAction.Create(
        UITableViewRowActionStyle.Normal,
        "activate",
        delegate {
            //activate or deactivate 
        }
    );

    UITableViewRowAction deleteNotifAction = UITableViewRowAction.Create(UITableViewRowActionStyle.Destructive,"Delete",async delegate
    {
        UIAlertController alert = UIAlertController.Create(_notifications.ElementAt(indexPath.Row).Label, "ConfirmDeleteAlert", UIAlertControllerStyle.Alert);
        alert.AddAction(UIAlertAction.Create("Yes", UIAlertActionStyle.Destructive, (action) => {
            _notifications.RemoveAt(indexPath.Row);
            tableView.DeleteRows(new NSIndexPath[] { indexPath }, UITableViewRowAnimation.Fade);
        }));
        alert.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, (action) => { }));
        await _AlertsViewController.PresentViewControllerAsync(alert, true);
    });
    return new UITableViewRowAction[] { deleteNotifAction, activateOrDeactivateNotifAction };

}

}
There, I have two actions, "activate" and "delete". Until here, everything works fine BUT, the action for deleting makes me an error. When I delete a row and after accepting the removing, it makes me NSInternalInconsistencyException error :

Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (7) must be equal to the number of rows contained in that section before the update (7), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
I think the error is correct, because the number of cells in Table must change, but in NotificationTableDelegate I have no access to items in UITableViewSource !! How can I resolve this error or how can I modify items in UITableViewSource ? Do you have any idea?

*I have the code for having delete inside UITableViewSource by using CommitEditingStyle etc... and it's not in my case. In additional I have CanEditRow, true in UITableViewSource.

Sign In or Register to comment.