0

だから私はテーブルビューで行を削除しようとしています。

これが私のコードです:

- (IBAction)done:(UIStoryboardSegue *)segue
{
    DetailGodsViewController *detailController = [segue sourceViewController];  
    NSIndexPath *path = [NSIndexPath indexPathForRow:detailController.row inSection:detailController.section];
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:path]
                     withRowAnimation:NO];
    [self.listOfGoods deleteGood: detailController.row];
    [[self tableView] reloadData];
    [self dismissViewControllerAnimated:YES completion:NULL];
}

StoryBoard に ControlViewTable があります。ControlViewTable の行をクリックすると、詳細ビ​​ューにジャンプします。その他の情報もあります。行とセクションに関する情報もこの関数に保存します。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"ShowGoodDetails"]) {
    DetailGodsViewController *detailViewController = [segue destinationViewController];

    detailViewController.row = [self.tableView indexPathForSelectedRow].row;
    detailViewController.section = [self.tableView indexPathForSelectedRow].section;
    detailViewController.good = [self.listOfGoods getGoodAtIndex:detailViewController.row ];
}

詳細ビューには削除用のボタンもあります。クリックすると、関数にジャンプします。

- (IBAction)done:(UIStoryboardSegue *)segue.

しかし、deleteRows では常にクラッシュします。誰か助けてくれませんか?

4

2 に答える 2

1

メソッド内のコードは、done:いくつかの順序が狂っていることに加えて、必要のないいくつかの追加のことを実行しています。そのはず:

- (IBAction)done:(UIStoryboardSegue *)segue
{
    DetailGodsViewController *detailController = [segue sourceViewController];  
    NSIndexPath *path = [NSIndexPath indexPathForRow:detailController.row inSection:detailController.section];
    [self.listOfGoods deleteGood: detailController.row];
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:path]
                     withRowAnimation:NO];
    [self dismissViewControllerAnimated:YES completion:NULL];
}

基本的に、テーブルを更新する前にデータを更新する必要があります。reloadDataまた、を呼び出した後にテーブルを呼び出さないでくださいdeleteRowsAtIndexPaths:。両方ではなく、どちらか一方を実行します。

于 2013-02-21T19:58:01.767 に答える
1

1 つの問題は、ボタンを含むセルを取り除こうとしているときに、まだボタンに応答していることです。そのアクション メソッドを終了させて​​から、deleteRowsを呼び出す必要があります。おそらく、ここで推奨するようなことを行う必要があります。

https://stackoverflow.com/a/13907375/341994

ただし、おそらく最大の問題は、テーブルの行を削除する前にモデル データを更新する必要があることです。

于 2013-02-21T19:54:03.397 に答える