c# - UITableViewDelegate 中删除单元格时发生 NSInternalInconsistencyException 错误/Xamarin.iOS

标签 c# ios uitableview xamarin.ios swipe

我希望在 Xamarin.iOS 的 TableView 中正常滑动(这意味着从右到左)有两个操作。

当我搜索时,我知道我可以为我的表自定义 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 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).

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

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

最佳答案

您可以使用tableview的Source来代替。

该类包含UITableViewDelegate + UITableViewDataSource中的所有方法

关于c# - UITableViewDelegate 中删除单元格时发生 NSInternalInconsistencyException 错误/Xamarin.iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47712144/

相关文章:

c# - 悖论 : Why is yield return faster than list here

ios - 并非所有意图都适用于 native iOS Twitter 应用程序

ios - 将 Firebase 的图像下载到 TableView

c# - 在 StateMachine 中使用 CallExternalMethodActivity/HandleExternalEventActivity

c# - SmtpClient.Send随机导致SmtpException 4.7.0等待客户端输入超时

ios - React Native IOS build clang : error: linker command failed with exit code 1 (use -v to see invocation)

ios - 跟踪数十个应用程序 ID 和配置文件的最佳方法是什么?

ios - 设置嵌入 UITableViewCell 中的 UICollectionView

ios - 在 View Controller 之间传递数据 DidSelectRowsAtIndexPath Storyboard

c# - Postgres analog for c# SQL Server´s stored procedure CLR