2010-04-01 5 views
14

J'ai un gros problème. J'essaie de créer un bouton favori sur chaque UITableViewCell dans un UITableView. Cela fonctionne très bien, et j'ai actuellement une action et un sélecteur effectué lorsqu'il est pressé.UITableViewCell Accessoire personnalisé - obtenez la rangée d'accessoires

accessory = [UIButton buttonWithType:UIButtonTypeCustom]; 
[accessory setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal]; 
accessory.frame = CGRectMake(0, 0, 15, 15); 
accessory.userInteractionEnabled = YES; 
[accessory addTarget:self action:@selector(didTapStar) forControlEvents:UIControlEventTouchUpInside]; 
cell.accessoryView = accessory; 

Et sélecteur:

- (void) didTapStar { 
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:/* indexPath? */]; 

    accessory = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [accessory setImage:[UIImage imageNamed:@"stared.png"] forState:UIControlStateNormal]; 
    accessory.frame = CGRectMake(0, 0, 26, 26); 
    accessory.userInteractionEnabled = YES; 
    [accessory addTarget:self action:@selector(didTapStar) forControlEvents:UIControlEventTouchDown]; 
    newCell.accessoryView = accessory; 
} 

Maintenant, voici le problème: je veux savoir quelle rangée l'accessoire qui a été pressé appartient. Comment puis-je faire cela?

Merci :)

Répondre

20

Je voudrais utiliser le code suivant:

- (void)didTapStar:(id)sender { 
    NSIndexPath *indexPath = [tableView indexPathForCell:(UITableViewCell *)[sender superview]]; 
} 
+0

Je l'utilise aussi. – ZYiOS

16

Changez votre action autour d'un peu.

- (void)action:(id)sender forEvent:(UIEvent *)event

intérieur de cette méthode:

NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:[[[event touchesForView:sender] anyObject] locationInView:tableView]];

Cela devrait vous obtenir le chemin d'index de la ligne.

+1

Merci beaucoup - qui fonctionne parfaitement! Je vous remercie! :) – Emil

+1

Quel est le rôle de 'anyObject' ici? Qu'est ce que ça fait ? – Illep

+0

C'est un truc génial! Je vous remercie. –

1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath { 
    // To set custom cell accessory      
    UIImage *image = [UIImage imageNamed:@"white_arrow_right.png"]; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    CGRect frame = CGRectMake(cell.frame.size.width - image.size.width, cell.frame.size.height - image.size.height, image.size.width, image.size.height); 
    button.frame = frame; 
    [button setBackgroundImage:image forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(accessoryButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside]; 
    button.backgroundColor = [UIColor clearColor]; 
    cell.accessoryView = button; 
}  

- (void)accessoryButtonTapped:(id)sender event:(id)event { 
    NSSet *touches = [event allTouches]; 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentTouchPosition = [touch locationInView:self.tableView]; 
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition]; 
    if (indexPath != nil){ 
     [self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath]; 
    } 
} 

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{     
    DetailViewController *detailView =[[DetailViewController alloc]init]; 
    [self.navigationController pushViewController:detailView animated:YES]; 
} 
+0

merci. fonctionne parfaitement – AmiiQo

Questions connexes