2

每当我点击 DeleteRows 代码时,我都会收到一个异常,告诉我更新前后的行数必须相同。以下是官方原文:

原因:无效更新:第 0 节中的行数无效。更新 (3) 后现有节中包含的行数必须等于更新前该节中包含的行数 (3),加减从该部分插入或删除的行数(0 插入,1 删除)。

我的代码是:

        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);
        }
    }
4

2 回答 2

1

您可能需要更改返回的数字

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

indexPath.section删除前低一。

如此处回答:从 uitableview 崩溃中删除行

于 2010-02-11T17:44:10.073 回答
0

我发现对我有用的是删除 tableView.DeleteRows(new [] { indexPath }, UITableViewRowAnimation.Fade); 你的方法应该是这样的。

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();
        }
    }
于 2018-02-13T13:04:34.103 回答