Question

I'm a beginner in iOS. I'm performing a swipe delete option. I want to display an alert view before deleting the row. How can i perform this action.

- (void)tableView:(UITableView *)tableView commitEditingStyle:
  (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@",collisionsArray);
if (editingStyle == UITableViewCellEditingStyleDelete)
{       
  NSUserDefaults *userinfo = [NSUserDefaults standardUserDefaults];
  NSString *userId = [userinfo valueForKey:@"user_id"];
  if(userId!=nil)
  {
  NSDictionary* dict = [collisionsArray objectAtIndex:indexPath.section];
  collisionId = [NSString stringWithFormat:@"%@",[dict valueForKey:@"collisionId"]];
  NSLog(@"%@",collisionId);
// removes saved datas from database
  BOOL result =  [database removeCollisionDetails:collisionId:@"accident_report"];
    if(result)
    {  
    [[SHKActivityIndicator currentIndicator] 
    displayCompleted:NSLocalizedString(@"val_sucess_vehicle", nil)];
    [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
    [[SHKActivityIndicator currentIndicator]
    displayCompleted:NSLocalizedString(@"val_error", nil)];
    }
  }
}
[self.tableView reloadData];
}
Was it helpful?

Solution

For this you can just display an alet view in:

if (editingStyle == UITableViewCellEditingStyleDelete){
 // Show your alert view
 // Set its delegate to self
}

Now you have to do something like:

#pragma mark ---- Delegate for alertview ----
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        NSUserDefaults *userinfo = [NSUserDefaults standardUserDefaults];
    NSString *userId = [userinfo valueForKey:@"user_id"];
    if(userId!=nil)
    {
        NSDictionary* dict = [collisionsArray objectAtIndex:indexPath.section];
        collisionId = [NSString stringWithFormat:@"%@",[dict valueForKey:@"collisionId"]];
        NSLog(@"%@",collisionId);
// removes saved datas from database
            BOOL result =  [database removeCollisionDetails:collisionId:@"accident_report"];
        if(result)
            {  
                [[SHKActivityIndicator currentIndicator] displayCompleted:NSLocalizedString(@"val_sucess_vehicle", nil)];
                [self.navigationController popViewControllerAnimated:YES];
            }



 else
        {
            [[SHKActivityIndicator currentIndicator] displayCompleted:NSLocalizedString(@"val_error", nil)];
        }
        }
    }

OTHER TIPS

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


if (editingStyle == UITableViewCellEditingStyleDelete) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Warring" message:@"Are You Sure?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes ", nil];


[alert show];



}


}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {


}else if (buttonIndex == 1){
     NSIndexPath *indexPath=[_UserTableView indexPathForSelectedRow];
    [showUData removeObjectAtIndex:indexPath.row]; //showUData NSMutableArray
    [_UserTableView reloadData];
    [storeData setObject:showUData forKey:@"SendData"]; //storeData NSUserDefault
    [storeData synchronize];
}

}

Use UIAlertView and only delete if the user confirms. Update your code to :

if (editingStyle == UITableViewCellEditingStyleDelete)
{       

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Hello World!"
                                                      message:@"Are you sure ?"
                                                     delegate:self
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];

[alertView show];
}

Implement the UIAlertViewDelegate and move the original delete code to the delegate method where you detect which button has been tapped.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top