2012-04-20 9 views
1

私はUITableViewControllerを持っています。ユーザーが行を削除できるように設定されています。アラート確認でUITableViewControllerの行を削除する

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"t" message:@"del?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Delete", nil]; 
     [alert show]; 
    } 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSIndexPath *path = [self.tableView indexPathForSelectedRow]; 
    if (buttonIndex != [alertView cancelButtonIndex]) { 
     //my code to delete from the data source is here. it works fine. 
     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:path] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 

問題は、実際にtableViewから行を削除するはずだと最後の行である:これは私の削除アクションのハンドラのコード、およびUIAlertViewためのデリゲートメソッドです。 UIAlertViewの代わりに、最初のメソッドでデータソースから削除したコードを置くとうまくいきます。

これを行う正しい方法は何ですか?

答えて

1

問題は、その方法の中でUIIndexPathオブジェクトが間違っていたことでした。アラートビューが表示されている間に行が選択されていないためだと思います。だから、UIAlertViewコードをビューコントローラクラスのプロパティに入れる前に保存し、後で2番目のメソッドで取得しても問題ありません。

関連する問題