2014-09-18 1 views
1

J'essaie de supprimer une ligne dans la vue de table après confirmation de l'utilisateur, en utilisant une vue d'alerte. Cependant, je ne sais pas comment laisser la méthode UIAlertViewDelegate savoir quelle ligne de la table à supprimer.Transmettre les informations au délégué UIAlertView

// 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 
    } 
} 

en méthode d'alerte i essayer gérer à supprimer la ligne de la table et base de données

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


    } 
+0

Qu'est-ce que la trace de la pile d'erreurs? Vous venez de poster du code et vous ne dites pas ce qui ne fonctionne pas – Foriger

+0

Et quel est le problème réel? BTW, en comparant les valeurs textuelles des boutons entravera vos tentatives de i18n. – trojanfoe

+0

je dois passer indexPath.row à délégué uialert –

Répondre

2

Si vous voulez passer quelque chose au délégué puis ajouter une propriété à la classe des délégués et transmettre les informations à avant d'invoquer l'affichage des alertes:

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

// @implementation AlertDelegate omitted 

Et utiliser ainsi:

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

Si le délégué est self, cela signifie ajouter la propriété à self (ou utiliser une variable d'instance privée).

Dans la méthode Delegate alors accès au indexPath:

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    NSString*title = [alertView buttonTitleAtIndex:buttonIndex]; 
    if ([title isEqualToString:@"YES"]) { 
     [names removeObjectAtIndex:self.indexPath.row]; 
    } 
} 
+0

self.indexPath.row .. comment vais-je l'obtenir ... ?? – Chandan

+0

@ChandanSingh Je ne comprends pas votre question. – trojanfoe

+0

[noms removeObjectAtIndex: self.indexPath.row]; J'ai un problème dans cette ligne .. – Chandan

1

Ajouter une variable à votre classe de contrôleur tableView pour maintenir la indexPath.row puis l'utiliser dans le alertview. Enregistrez indexPath.row avant d'afficher l'alerte.

vous devez également appeler

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

après avoir vérifié que l'utilisateur sélectionné OUI dans la méthode déléguée alertView.

1

Au lieu UIAlertView utiliser CustomAlertview CustomAlertView * lAlert = [[CustomAlertView alloc] ..... initialisation

#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 

Dans la méthode délégué obtenir les données sélectionnées ou index comme ci-dessous

- (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; 
} 
Questions connexes