UITableViewDelegate中的NSInternalInconsistencyException错误,用于删除单元格/ Xamarin.iOS

时间:2017-12-08 10:15:01

标签: c# ios uitableview xamarin.ios swipe

我想在Xamarin.iOS的表格视图中以滑动法线(这意味着从右到左)有两个动作。

当我搜索时,我知道我可以为我的桌子自定义UITableViewDelegate并使用不同的动作进行滑动。

这是我的UITableViewDelegate代码:

public class NotificationTableDelegate : UITableViewDelegate
{
    private readonly AlertsViewController _AlertsViewController;
    private List<NotificationItem> _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 };

    }
}

在那里,我有两个动作,“激活”和“删除”。直到这里,一切正常但是,删除操作让我错误。 当我删除一行并接受删除后,它会让我产生NSInternalInconsistencyException错误:

  

抛出Objective-C异常。名称:NSInternalInconsistencyException   原因:无效更新:第0部分中的行数无效   更新后现有部分中包含的行数(7)   必须等于之前该部分中包含的行数   更新(7),加上或减去插入或删除的行数   从该部分(0插入,1删除)和加号或减号   移入或移出该部分的行(0移入,0移出)。

我认为错误是正确的,因为Table中的单元格数量必须更改,但在NotificationTableDelegate中我无权访问UITableViewSource中的项目!如何解决此错误或如何修改UITableViewSource中的项目?任何的想法?

*我有使用CommitEditingStyle等在UITableViewSource中删除的代码......而且不是我的情况。 另外我有CanEditRow,在UITableViewSource中为true。

1 个答案:

答案 0 :(得分:3)

您可以改用tableview的Source

此课程包括UITableViewDelegate + UITableViewDataSource

中的所有方法
相关问题