代码之家  ›  专栏  ›  技术社区  ›  Driss Zouak

在TableView中删除行时出现异常

  •  1
  • Driss Zouak  · 技术社区  · 16 年前

    每当我点击deleterrows代码时,我会得到一个异常,告诉我更新前后的行数必须相同。这是官方文本:

    原因:更新无效:节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 回复  |  直到 8 年前
        1
  •  1
  •   Community Mohan Dere    9 年前

    您可能需要更改返回的号码

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

    对于 indexPath.section 比删除前低一个。

    如本文所述: Delete row from uitableview crashes

        2
  •  0
  •   Jermal Prestwood    8 年前

    我发现对我有用的是移除 tableview.deleterrows(新建[]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();
            }
        }
    
    推荐文章