简体   繁体   English

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

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

I'd like to have two actions in swipe normal (it means right to left), in a table view in Xamarin.iOS. 我想在Xamarin.iOS的表格视图中以普通方式滑动两个动作(即从右到左)。

As I searched, I know that I can customize a UITableViewDelegate for my table and have a swipe with different actions. 在搜索时,我知道可以为表自定义UITableViewDelegate并使用不同的动作进行滑动。

Here is my UITableViewDelegate code : 这是我的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 };

    }
}

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 : 当我删除一行并接受删除后,它使我NSInternalInconsistencyExceptionException错误:

Objective-C exception thrown. 抛出Objective-C异常。 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). 名称:NSInternalInconsistencyException原因:无效的更新:第0节中的行数无效。更新(7)之后,现有节中包含的行数必须等于更新(7)前该节中包含的行数,加上或减去从该部分插入或删除的行数(已插入0,已删除1),再加上或减去该部分移入或移出的行数(移入0,移出0)。

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 !! 我认为错误是正确的,因为表中的单元格数必须更改,但是在NotificationTableDelegate中,我无法访问UITableViewSource中的项目! How can I resolve this error or how can I modify items in UITableViewSource ? 如何解决此错误或如何修改UITableViewSource中的项目? Any idea? 任何想法?

*I have the code for having delete inside UITableViewSource by using CommitEditingStyle etc... and it's not in my case. *我有使用CommitEditingStyle等在UITableViewSource内部进行删除的代码...在我看来,这不是。 In additional I have CanEditRow, true in UITableViewSource. 另外,我还有CanEditRow,在UITableViewSource中为true。

You can use Source of tableview instead. 您可以改用tableview的Source

This class include all the methods in UITableViewDelegate + UITableViewDataSource 此类包含UITableViewDelegate + UITableViewDataSource所有方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM