2010-04-01 6 views
14

Ho un grosso problema. Sto cercando di creare un pulsante preferito su ogni UITableViewCell in un UITableView. Funziona molto bene, e al momento ho un'azione e un selettore eseguito quando premuto.UITableViewCell Accessorio personalizzato - ottieni la fila di accessori

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; 

E selettore:

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

Ora, ecco il problema: voglio sapere cosa riga l'accessorio che è stato premuto appartiene. Come posso fare questo?

Grazie :)

risposta

20

vorrei utilizzare il seguente codice:

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

Anche io lo uso. – ZYiOS

16

Cambia la tua azione un po '.

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

Dentro quel metodo:

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

Questo dovrebbe farti percorso dell'indice della riga.

+1

Grazie mille - che funziona perfettamente! Grazie! :) – Emil

+1

Qual è il ruolo di 'anyObject' qui? Che cosa fa ? – Illep

+0

Questo è un trucco fantastico! Grazie. –

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

grazie. funziona perfettamente – AmiiQo

Problemi correlati