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];
    }
}

编辑:这应该编译,尽管可能有轻微的语法错误。一般假设:您只处理一个部分。至少只有一个部分在删除是可能的。

于 2013-01-12T20:54:19.760 回答
5

iOS 8 +

iOS 8 推出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

iOS 9.0 和 Swift 2.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 回答