8

テーブルビューセルを削除したいのですが、そのアクションが発生する前に、ユーザーにアラートビューを提供したいと思います。私はこれを得た:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning"
                                                        message:@"Are you sure?"
                                                       delegate:self
                                              cancelButtonTitle:@"NO"
                                              otherButtonTitles:@"YES", nil];
        [alert show];

        [self.array removeObjectAtIndex:indexPath.row];//or something similar to this based on your data source array structure
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Nee"])
    {
        NSLog(@"Nothing to do here");
    }
    else if([title isEqualToString:@"Ja"])
    {
        NSLog(@"Delete the cell");

    }
}

しかし、セルを右にスワイプして削除ボタンが表示された場合、AlertViewが表示されません。削除ボタンを押したときにのみAlertViewが表示されます。削除ボタンを押すとメッセージが表示されますが、セルはすでに削除されています。

これを機能させる方法は?したがって、スワイプするとAlertViewが表示されます。

4

4 に答える 4

17

シーケンスに関しては、すべてが順調です。commitEditingStyle削除ボタンがすでに押されている場合にのみ呼び出されます。重要なのは、アラートに応答する前に実際にオブジェクトを削除しているということです。これに変更します:

これを.mファイルの前に追加します@implementation

@interface PutYourViewControllerClassNameHere
@property (strong, nonatomic) NSIndexPath *indexPathToBeDeleted;
@end

その後:

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

        self.indexPathToBeDeleted = indexPath;

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning"
                                                        message:@"Are you sure?"
                                                       delegate:self
                                              cancelButtonTitle:@"NO"
                                              otherButtonTitles:@"YES", nil];
        [alert show];
        // do not delete it here. So far the alter has not even been shown yet. It will not been shown to the user before this current method is finished.     
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // This method is invoked in response to the user's action. The altert view is about to disappear (or has been disappeard already - I am not sure) 

    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"NO"])
    {
        NSLog(@"Nothing to do here");
    }
    else if([title isEqualToString:@"YES"])
    {
        NSLog(@"Delete the cell");

        [self.array removeObjectAtIndex:[self.indexPathToBeDeleted row]];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.indexPathToBeDeleted] withRowAnimation:UITableViewRowAnimationFade];
    }
}

編集:おそらくマイナーな構文エラーにもかかわらず、これはコンパイルされるはずです。一般的な仮定:1つのセクションのみを扱っています。削除内の少なくとも1つのセクションでのみ可能です。

于 2013-01-12T20:54:19.760 に答える
5

iOS 8 +

iOS8が導入されましUIAlertControllerた。これにより、デリゲートメソッドではなく完了ブロックに削除およびキャンセルコードを記述できます(-clickedButtonAtIndex旧式のようにUIAlertView)。

スウィフト3

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        let alertController = UIAlertController(title: "Warning", message: "Are you sure?", preferredStyle: .alert)

        let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: { (action) in
            self.tableView.deleteRows(at: [indexPath], with: .fade)
        })
        alertController.addAction(deleteAction)

        let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
        alertController.addAction(cancelAction)

        present(alertController, animated: true, completion: nil)
    }
}

Objective-C

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Warning" message:@"Are you sure?" preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }];
        [alertController addAction:deleteAction];

        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"Don't do anything");
        }];
        [alertController addAction:cancelAction];

        [self presentViewController:alertController animated:YES completion:nil];
    }
}
于 2016-03-21T06:54:50.413 に答える
2

削除アクションがすでに実行されているときにアラートを呼び出しています。

中に入れて:

-(void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Waarschuwing!"
                                                message:@"Weet je zeker dat je het vak: lalaal wilt verwijderen?"
                                               delegate:self
                                      cancelButtonTitle:@"Nee"
                                      otherButtonTitles:@"Ja", nil];
[alert show];
}

これにより、セルがスワイプされたとき、ボタンが押される前にアラートが呼び出されます。

于 2013-01-12T20:58:48.417 に答える
0

iOS9.0およびSwift2.3

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    if editingStyle == .Delete {

        let alertController = UIAlertController(title: "Warning!", message: "You're about to delete this stuff right meow.", preferredStyle: .Alert)
        let delete = UIAlertAction(title: "Do it.", style: .Destructive, handler: { action in

            tableView.beginUpdates()
            //delete from your datasource!
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
            tableView.endUpdates()
        })

        let cancel = UIAlertAction(title: "Cancel", style: .Cancel, handler: { action in

            //this is optional, it makes the delete button go away on the cell
            tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
        })

        alertController.addAction(delete)
        alertController.addAction(cancel)
        presentViewController(alertController, animated: true, completion: nil)
    }
}
于 2016-08-16T19:08:40.673 に答える