1

I'm trying to delete a row in table view after confirmation from the user, using an alert view. However I don't know how to let the UIAlertViewDelegate method know which row in the table to delete.

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        UIAlertView *alert_delete = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"Confirm Delete %@",[names objectAtIndex:indexPath.row] ] message:@"Warning all student data will be earsed" delegate:self cancelButtonTitle:@"Dismess" otherButtonTitles:@"YES", nil];
        [alert_delete show];

        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

in alert method i try handle it to delete row from table and database

    -(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
        NSString*title = [alertView buttonTitleAtIndex:buttonIndex];
        if ([title isEqualToString:@"YES"]) {
           // how to pass indexPath.row to alertview 
             [names removeObjectAtIndex:indexPath.row];
        }


    }
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
Mohammad Shaker
  • 185
  • 1
  • 15

4 Answers4

2

If you want to pass something to the delegate then add a property to the delegate class and pass the information to it before invoking the alert view:

@interface AlertDelegate : NSObject <UIAlertViewDelegate>
@property (nonatomic) NSIndexPath *indexPath;
@end

// @implementation AlertDelegate omitted

And use thus:

UIAlertView *alertView = ...;
AlertDelegate *delegate = [AlertDelegate new];
alertView.delegate = delegate;
delegate.indexPath = indexPathFromSomewhere;
[alertView show];   // or whatever

If the delegate is self, then that means adding the property to self (or use a private instance variable).

In the delegate method you then have access to the indexPath:

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString*title = [alertView buttonTitleAtIndex:buttonIndex];
    if ([title isEqualToString:@"YES"]) {
        [names removeObjectAtIndex:self.indexPath.row];
    }
}
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
1

Add a variable to your tableView controller class to hold the indexPath.row and then use it in the alertview. Save indexPath.row in it before showing the alert.

Also you need to call

 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

after confirming that user selected YES in the alertView delegate method.

jamihash
  • 1,900
  • 1
  • 12
  • 15
1

You can also use objc_setAssociatedObject if info to be passed is a collection object. link - How do you pass a variable to the UIAlertView delegate?

Community
  • 1
  • 1
Srinivasan N
  • 611
  • 9
  • 20
1

Instead UIAlertview use CustomAlertview CustomAlertView *lAlert = [[CustomAlertView alloc] init.....

#import <UIKit/UIKit.h>

@interface CustomAlertView : UIAlertView
@property (nonatomic,retain)NSString *mIndex;
@end
#import "CustomAlertView.h"

@implementation CustomAlertView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

In the delegate method get the selected data or index as below

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        NSLog(@"index %@",((CustomAlertView *)alertView).mIndex);
    }

In the table delegate method assing the index or data as below 
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
 CustomAlertView *lAlert = [[CustomAlertView alloc] .....
lAlert.mIndex = @"1";
  [lAlert show];
     lAlert = nil;
 }
Ramesh Muthe
  • 811
  • 7
  • 15