0

UITableViewCellEditingStyleDeleteを使用してUITableViewから行を削除しようとすると、アサーションエラーが発生しました。誰かが私のコードで何がうまくいかないのか教えてくれることを願っています。

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:1070
2013-01-30 14:19:21.450 MyApp[48313:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
*** First throw call stack:
(0x28ee012 0x1d3fe7e 0x28ede78 0x19fef35 0xf51b8d 0xf5df95 0xf5dfc3 0xcfe9 0xf6c384 0x10a9b1b 0x1d53705 0xeb3920 0xeb38b8 0xf74671 0xf74bcf 0xf746a6 0x1144f95 0x1d53705 0xeb3920 0xeb38b8 0xf74671 0xf74bcf 0xf73d38 0xee333f 0xee3552 0xec13aa 0xeb2cf8 0x2d1adf9 0x2d1aad0 0x2863bf5 0x2863962 0x2894bb6 0x2893f44 0x2893e1b 0x2d197e3 0x2d19668 0xeb065c 0x24fd 0x2435)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

これが私のコードです:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [records count];
}

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

        // Delete record from the sqlite database
        NSNumber *recordDBID = [records objectAtIndex:indexPath.row];
        Record *myRecord = [[Record alloc] initWithDBID:recordDBID database:UIAppDelegate.formManager.connection];
        BOOL valueX = [myRecord deleteRecordInDb:recordDBID];
        [myRecord release];

        // Delete record from array
        NSMutableArray *aNewArray = [[NSMutableArray alloc] initWithArray:records];
        [aNewArray removeObjectAtIndex:indexPath.row];
        records = aNewArray;
        [aNewArray release];

        // Delete the record from the table
        [tableView beginUpdates];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView endUpdates];
        [tableView reloadData];   
}
4

2 に答える 2

0

テーブルビューの行の削除が複雑になりすぎている可能性があると思います。

呼び出す必要はありませ。これら2つの呼び出しの間の単一の行のみを削除する場合は、それらを失う可能性があります。更新の開始と終了は、複数の挿入または削除アクションを同時に実行する場合にのみ必要です。[tableView beginUpdates][tableView endUpdates]

次に、[tableView reloadData]テーブルビューは、割り当てられたデリゲート/データソースを使用して削除アクションの一部として必要な情報を自動的に要求するため、この時点での呼び出しはやり過ぎです。コードの最後の部分を次のように減らすことができます。

//テーブルからレコードを削除します

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

現在の行削除コード(より広いコードベースとの組み合わせ)がシステムを混乱させている可能性があります。

上記が役に立たない場合は、records配列が変更されているすべてのコードを確認する必要があります。このイベントでは、私の答えを更新します。

于 2013-01-30T08:16:54.427 に答える
0

私の場合、タイプミスがあります。indexPath代わりnewIndexPathinsertRowsAtIndexPaths

 func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?)
        {
            switch type
            {
            case .Insert:
                self.tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Bottom)

            case .Delete:
                self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .None)

            case .Update:
                self.tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)

            case .Move:
                    self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .None)
                    self.tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Bottom)
                }
        }
于 2015-05-19T07:45:23.727 に答える