在TableView中删除行时出现异常

时间:2010-02-11 17:38:45

标签: iphone xamarin.ios tableview

每当我点击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);
        }
    }

2 个答案:

答案 0 :(得分:1)

您可能需要更改

中返回的数字
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section

indexPath.section比删除前低一个。

在这里回答:Delete row from uitableview crashes

答案 1 :(得分: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();
        }
    }