0

次のメソッドを使用してテーブルビュークラスで、テーブルビューの行を削除しようとしています

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

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

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:YES];   
}

- (void)tableView:(UITableView *)tv commitEditingStyle (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if(editingStyle == UITableViewCellEditingStyleDelete) 
    {
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [arrUserData removeObjectAtIndex:indexPath.row];
    }
}

そして、テーブルに約40行あります。行のいずれかで赤い削除ボタンをクリックすると、この行でクラッシュします

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

次のクラッシュログ

2012-05-30 14:58:45.835 testDeleteRow[3276:207] *** 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 (41) must be equal to the number of rows contained in that section before the update (41), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'

どうすればこれを修正できるか教えてもらえますか、事前に感謝します

4

2 に答える 2

1

実際には、削除するときにデータ ソースを更新する必要がありますcells。入力に ​​を使用している場合arraytableこの行の後に -

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

indexから同時にオブジェクトを削除する必要がありますarray。また、行数をsection1 減らす必要があります。count を返す場合はarray、自動的に処理されます。

于 2012-05-30T10:07:26.963 に答える
0

テーブルビューから行を削除すると、データソースは更新されません。削除前にテーブルに10行が含まれている場合、削除後は9行しか含まれませんが、データソースにはまだ10個の値が含まれています。したがって、削除されたものを削除する必要がありますデータ ソースから明示的に値を取得します。

于 2012-05-30T10:17:17.227 に答える