2010-04-01 16 views
14

Tengo un problema bastante grande. Estoy intentando crear un botón favorito en cada UITableViewCell en un UITableView. Eso funciona muy bien, y actualmente tengo una acción y un selector ejecutados cuando me presionan.Accesorio UITableViewCell personalizado: obtenga la fila del accesorio

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; 

y el selector:

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

Ahora, aquí está el problema: Quiero saber qué fila del accesorio que se presionó pertenece. ¿Cómo puedo hacer esto?

Gracias :)

Respuesta

20

me gustaría utilizar el siguiente código:

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

Yo uso esto también. – ZYiOS

16

Cambie su acción un poco.

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

Dentro de ese método:

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

Eso debe conseguirle ruta del índice de la fila.

+1

Muchas gracias - que funciona perfectamente! ¡Gracias! :) – Emil

+1

¿Cuál es el rol de 'anyObject' aquí? Qué hace ? – Illep

+0

¡Este es un truco increíble! Gracias. –

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

gracias. funciona perfectamente – AmiiQo

Cuestiones relacionadas