1

UITableView でセクションの折りたたみをシミュレートするために行を削除しようとすると、次のエラー メッセージが表示されます。

 *** 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 (3) 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, 3 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

このメソッドを使用して行を削除しています。呼び出し元はセクションのヘッダー ビューであり、そのタグはそのセクションです。

- (IBAction)toggleExpand:(id)sender
{
int section = [sender tag];

if (expandedArray[section]) {
    expandedArray[section] = NO;
    NSMutableArray *rowsToDelete = [[NSMutableArray alloc] init];
    for (int i = 0; i < 3; i++) {
        [rowsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:section]];
    }
    [mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithArray:rowsToDelete] withRowAnimation:UITableViewRowAnimationAutomatic];

    }
}

行を削除した後にデータ ソースを更新していないことが問題のようですが、メソッド numberOfRowsInSection でそれを実行しようとしています。

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (expandedArray[section]) {
        return 3;
    }
    else {
        return 0;
    }

}

ExpandedArray は bool の配列であり、すべてのセクションがデフォルトで展開されるため、すべて YES として開始されます。

編集:いくつかのデバッグ メッセージを追加し、次のメッセージを受け取りました。

Asking number of rows....
Section 1 is expanded.
Asking number of rows....
Section 0 is expanded.
header section: 0
header section: 1
toggleExpand tag reads: 0
Asking number of rows....
Section 0 is expanded.
Asking number of rows....
Section 1 is not expanded.

私を悩ませているのは、セクション 0 であるべきときにセクション 1 が折りたたまれていると考えていることです。

4

0 に答える 0