2010-02-11 31 views
1

每当我点击DeleteRows代码时,都会得到一个异常,告诉我更新前后的行数需要相同。以下是官方文本:在TableView中删除行时出现异常

原因:无效更新:节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); 
     } 
    } 

回答

1

您可能需要更改

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

返回的数字为indexPath.section比删除前下一个。

由于这里回答:Delete row from uitableview crashes

+0

我能够从你所说的我能够实现我的DeleteRows和RemoveAt在错误的顺序。一旦我改变了他们的订单,那很好。谢谢! –

0

我发现,工作对我来说是去除 tableView.DeleteRows(新[] {} 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(); 
     } 
    }