Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios deleteRowsAtIndexPaths:withRowAnimation:不工作_Ios_Uitableview - Fatal编程技术网

Ios deleteRowsAtIndexPaths:withRowAnimation:不工作

Ios deleteRowsAtIndexPaths:withRowAnimation:不工作,ios,uitableview,Ios,Uitableview,我正在尝试很好地删除mu UITableView的单元格: - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [

我正在尝试很好地删除mu UITableView的单元格:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        [self deleteModelForIndexPath:indexPath];

        [mainTableView beginUpdates];
        [mainTableView deleteRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimationTop];
        [mainTableView endUpdates];

    }
}
这将引发以下异常:

 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections.  The number of sections contained in the table view after the update (2) must be equal to the number of sections contained in the table view before the update (3), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'
我没有得到
(0插入,0删除)
部分。为什么它不起作用


谢谢

您应该同时更新表格数据。如果对每行使用数组对象,请从数组中删除已删除的对象。

错误的意思是:

更新后表视图中包含的节数(2)必须等于更新前表视图中包含的节数(3),加上或减去插入或删除的节数(0插入,0删除)。'

这可能是其中一条更好的错误消息,它基本上意味着支持表视图的数据量与其在UI中的状态不同步,特别是删除前后的节数(提示:check
–numberOfSectionsInTableView:
)。如果从表中删除行/节,则需要确保从支持该表的数据中删除该行/节

(插入0,删除0)

意味着API没有发现您的数据源有任何已删除的节

你是否绝对确定它正在做你认为它正在做的事情:

[self deleteModelForIndexPath:indexPath];

对于此类操作,不应使用
beginUpdates
endUpdates
方法。你只需做:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete)
{
        [self deleteModelForIndexPath:indexPath];

        [mainTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }
}
您还可以查看有关
beginUpdates
的文档:

如果需要后续的插入、删除和删除,请调用此方法 选择操作(例如,
cellforrowatinexpath:
indexPathsForVisibleRows
)同时设置动画。这群 方法必须以调用EndUpdate结束。这些方法 对可以嵌套。如果不进行插入、删除和 选择在此块内调用表属性,如行计数 可能会失效。您不应在组内调用
重新加载数据
; 如果在组内调用此方法,则需要执行任何 自己制作动画


似乎问题出在[self-deleteModelForIndexPath:indexPath]中;是否确实已从数据源中删除模型?是否可以共享
[self-deleteModelForIndexPath:indexPath]
?如果我执行“NSLog(@“%@)”,@([model count]);”在“[self-deleteModelForIndexPath:indexPath];”之前和之后,它打印x和x-1。。。这是一个非常复杂的方法,所以我想它比任何东西都更令人困惑p然后要检查的另一件事是检查您的
numberofrowsinssection
方法,以确保它在前后返回正确的内容。虽然您在技术上是正确的,开始/结束更新括号不是必需的,但在这种情况下它也不会改变任何内容。