Iphone 删除TableView中的行时出现异常
每当我点击DeleteRows代码时,就会出现一个异常,告诉我更新前后的行数必须相同。以下是官方文本: 原因:无效更新:节0中的行数无效。更新(3)后现有节中包含的行数必须等于更新(3)前该节中包含的行数,加上或减去从该节中插入或删除的行数(插入0,删除1) 我的代码是:Iphone 删除TableView中的行时出现异常,iphone,xamarin.ios,tableview,Iphone,Xamarin.ios,Tableview,每当我点击DeleteRows代码时,就会出现一个异常,告诉我更新前后的行数必须相同。以下是官方文本: 原因:无效更新:节0中的行数无效。更新(3)后现有节中包含的行数必须等于更新(3)前该节中包含的行数,加上或减去从该节中插入或删除的行数(插入0,删除1) 我的代码是: public override void CommitEditingStyle (UITableView tableView, UITableViewCellEditingStyle editingStyle,
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);
}
}
您可能需要更改中返回的号码
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section
使indexPath.section
比删除前低一个
正如这里回答的那样:我发现对我有效的方法是删除 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();
}
}
从你说的话中,我意识到我的DeleteRows和RemoveAt的顺序不对。一旦我改变了他们的顺序,一切都很好。谢谢