2016-04-21 4 views
1

Salut je travaille avec la tableview.Now je fais face à un problème. dans ma vue de table quand j'utilise le long geste je dois montrer l'alerte View.it fonctionne bien. quand je clique sur l'index de bouton 0 dans la vue d'alerte j'ai besoin d'effectuer une tâche. Mais j'ai besoin d'indexpath. Voici ma méthode pour effectuer des tâchesComment trouver le chemin d'index sur alertview?

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
             initWithTarget:self action:@selector(messageDeleteOrForword:)]; 
lpgr.minimumPressDuration = 2.0; //seconds 
lpgr.delegate = self; 
[self.tableView addGestureRecognizer:lpgr]; 

} 
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 

if (buttonIndex == 0) { 
    [self deleteSpecificMessage]; 

} 
if (buttonIndex==1) { 

} 
} 

-(void)messageDeleteOrForword:(UILongPressGestureRecognizer *)gestureRecognizer 
{ 
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Delete" message:@"Do you want to delete specific message" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Forword", nil]; 
[alert show]; 
} 


-(void)deleteSpecificMessage 
{ 

CGPoint p = [gestureRecognizer locationInView:self.tableView]; 
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p]; 
} 

mais je dois passer un paramètre à connaître le indexPath pour que j'utiliser en dessous de la ligne - (void) deleteSpecificMessage: (id) sender { } Mais comment appeler et assigner un paramètre dans alertview Aidez-moi s'il vous plaît.

+0

Post un code pour le geste long s'il vous plaît. –

+0

pouvez-vous s'il vous plaît vérifier mis à jour un – Bittoo

Répondre

0

Vous pouvez obtenir indexPath en utilisant

NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow]; 

Vous pouvez vérifier plus de détails à partir this link

Aussi, vous pouvez prendre une variable IndexPath dans le fichier .h qui peut être attribué lors de la sélection de la cellule de vue de table.

Tout d'abord Ajoutez votre outil de reconnaissance de gestes longs.

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(actionLongPressGeature:)]; 
[longPressGesture setDelegate:self]; 
[longPressGesture setMinimumPressDuration:0.3]; 
[tableView addGestureRecognizer:longPressGesture]; 

i.e. Option 1

-(void)actionLongPressGeature:(UILongPressGestureRecognizer *)gestureRecognizer 
{ 
    CGPoint p = [gestureRecognizer locationInView:tableViewJobs]; 
    // You need to declare NSIndexPath *indexPathSelected in your .h File 
    indexPath = [tableView indexPathForRowAtPoint:p]; 
    if (indexPath == nil) { 
     NSLog(@"long press on table view but not on a row"); 
    } 
    else if (gestureRecognizer.state == UIGestureRecognizerStateBegan) 
    { 
      // Perform your action i.e AlertView 
    } 
} 

Option 2:

-(void)actionLongPressGeature:(UILongPressGestureRecognizer *)gestureRecognizer 
{ 
     // You need to declare NSIndexPath *indexPathSelected in your .h File 
    indexPath = [tableView indexPathForSelectedRow]; 
    if (indexPath == nil) { 
     NSLog(@"long press on table view but not on a row"); 
    } 
    else if (gestureRecognizer.state == UIGestureRecognizerStateBegan) 
    { 
     // Perform your action i.e AlertView 
    } 
} 

et plus tard vous pouvez utiliser cette variable.

J'espère que c'est ce que vous cherchez.

+1

S'il vous plaît accepter ma question c'est l'utilisation complète pour quelqu'un d'autre Merci – Bittoo

0

Dans votre méthode de suppression, vous pouvez ajouter ceci:

CGPoint location = [gestureRecognizer locationInView:self.tableView]; 
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location]; 
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
+0

mais qu'en est-il de gestureRecognizer? – Bittoo

1

Afficher la vue d'alerte de didselectrowatindexpath. Définir la balise alertview en tant que chemin d'index à partir de cette méthode. De cette façon, vous allez intégrer votre alertview avec indexpath. Dans delegate alertview vous pouvez avoir indexpath comme c'est tag. Vous pouvez donc utiliser cette balise pour la supprimer ou la transférer en considérant le chemin d'index.

Parexemple:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Delete" message:@"Do you want to delete specific message" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Forword", nil]; 

alert.tag = indexPath; //setting tag 

[alert show]; 
} 

Mise à jour (comme demander en commentaire):

vous pouvez faire quelque chose comme ça, voici un exemple,

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
             initWithTarget:self action:@selector(handleLongPress:)]; 
lpgr.minimumPressDuration = 2.0; //seconds 
lpgr.delegate = self; 
[self.myTableView addGestureRecognizer:lpgr]; 

ici méthode handleLongpress

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer 
{ 
CGPoint p = [gestureRecognizer locationInView:self.myTableView]; 

NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:p]; 
if (indexPath == nil) { 
    NSLog(@"long press on table view but not on a row"); 
} 
else{ 

    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Delete" message:@"Do you want to delete specific message" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Forword", nil]; 

    alert.tag = indexPath; //setting tag 

    [alert show]; 
} 

} 

espère que cela aidera :)

+0

ok mais j'ai besoin quand j'utilise le robinet long dans ce cas? – Bittoo

+0

Vérifier ma mise à jour dans la réponse – Lion

+0

CGPoint p = [gestureRecognizer locationInView: self.myTableView]; Vous obtiendrez CGPoint à partir de la vue de table, maintenant que la hauteur de ligne vous permet de calculer l'index et ce qui doit être tapé depuis longtemps. – Hasya

0
-(void)messageDeleteOrForword:(UILongPressGestureRecognizer *)gestureRecognizer 
{ 
    CGPoint p = [gestureRecognizer locationInView:self.myTableView]; 

    NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:p]; 
    if (indexPath == nil) { 
     NSLog(@"long press on table view but not on a row"); 
    } 
    else{ 

     NSLog(@"selected row index %ld",(long)indexPath.row); 

     UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Delete" message:@"Do you want to delete specific message" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Forword", nil]; 
     [alert show]; 
    } 


} 
0

Vous pouvez également utiliser les objets associés Objective-C pour les propriétés d'exécution. Cette fonctionnalité est disponible en <objc/runtime.h>.Par exemple:

#import <objc/runtime.h> 

-(void)messageDeleteOrForword:(UILongPressGestureRecognizer *)gestureRecognizer 
{ 
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Delete" message:@"Do you want to delete specific message" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Forword", nil]; 
    objc_setAssociatedObject(alert, @"currentIndexPath", indexPath, OBJC_ASSOCIATION_RETAIN); 
    [alert show]; 
} 


- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    NSIndexPath *idxPath = objc_getAssociatedObject(alertView, @"currentIndexPath"); 
    NSLog(@"%@",idxPath); 

    if (buttonIndex == 0) { 
     [self deleteSpecificMessage]; 
    } 

    if (buttonIndex==1) { 
    } 
} 

Voici quelques liens qui sont plus utiles. En utilisant l'objet associé, vous pouvez associer plusieurs propriétés à n'importe quel objet et obtenir facilement. Dans le premier lien, vous pouvez également personnaliser les propriétés avec la définition de la catégorie.