在TableView中删除行时异常
-
20-09-2019 - |
题
每当我打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);
}
}
解决方案
您可能需要更改
返回的数字- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section
为indexPath.section
为比删除之前下一个。
如这里回答:删除行从UITableView的崩溃
其他提示
我发现,工作对我来说是取消 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();
}
}
不隶属于 StackOverflow