2011-09-17 13 views
6

Ho una vista tabella con due azioni, per la prima volta utilizzo il delegato didSelectRowAtIndexPath, per la seconda vorrei usare un'azione su un pulsante della mia cella per fare qualcos'altro. Il mio problema è che non riesco ad ottenere il percorso index? Qual è la migliore pratica per farlo.Come ottenere indexPath in una vista tabella da un'azione del pulsante in una cella?

+0

possibile duplic mangiato di [IOS 7 - Come ottenere indexPath dal pulsante posizionato in UITableViewCell] (http://stackoverflow.com/questions/19000356/ios-7-how-to-get-the-indexpath-from-button-placed- in-uitableviewcell) –

risposta

9

Se è stato aggiunto il pulsante alla cella come,

[cell.contentView addSubview:button]; 

allora, è possibile ottenere il percorso indice come,

- (void)onButtonTapped:(UIButton *)button { 

    UITableViewCell *cell = (UITableViewCell *)button.superview.superview; 
    NSIndexPath *indexPath = [tableView indexPathForCell:cell]; 
    // Go ahead 
} 
+0

Grazie! UIButton * button = (UIButton *) mittente; UITableViewCell * cell = (UITableViewCell *) button.superview.superview; –

+0

OK! Benvenuto! – EmptyStack

+1

Non si deve accedere alla tabellaView dal superview. Mentre questo funziona ora potrebbe non funzionare su un nuovo iOS. Esempio classico da 6/7 Apple ha aggiunto un'altra superview. Questo si bloccherebbe – CW0007007

1

qui è il codice completo per un pulsante personalizzato:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    CustomCellFilter *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (!cell) {cell = [[CustomCellFilter alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];} 

    //configure your cell here 
    cell.titleLabel.text = @"some title"; 

    //add button 
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    resetButton.frame = CGRectMake(40, 10, 18, 18); 
    [resetButton addTarget:self action:@selector(onButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
    [resetButton setImage:[UIImage imageNamed:@"someimage.png"] forState:UIControlStateNormal]; 
    [cell.contentView addSubview:myButton]; 

    //afterwards return the cell 
    return cell; 

- (void)onButtonTapped:(UIButton *)button { 

    UITableViewCell *cell = (UITableViewCell *)button.superview.superview; 
    NSIndexPath *indexPath = [filterTable indexPathForCell:cell]; 
    NSLog(@"%@", indexPath); 
    //use indexPath here... 
} 
-1

Nel caso in cui il pulsante viene spostato o Apple modifica la gerarchia di viste per le viste accessorie, questo metodo aumenta la catena per apparire per un UITableViewCell:

- (void)tapAccessoryButton:(UIButton *)sender { 
    UIView *parentView = sender.superview; 

// the loop should take care of any changes in the view heirarchy, whether from 
// changes we make or apple makes. 
    while (![parentView.class isSubclassOfClass:UITableViewCell.class]) 
     parentView = parentView.superview; 

    if ([parentView.class isSubclassOfClass:UITableViewCell.class]) { 
     UITableViewCell *cell = (UITableViewCell *) parentView; 
     NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
     [self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath]; 
    } 
} 
2

sto usando questa soluzione - ottenendo percorso dell'indice di cellulari utilizzando i punti

CGPoint center= [sender center]; 
CGPoint rootViewPoint = [[sender superview] convertPoint:center toView:_tableView1]; 
NSIndexPath *indexPath = [_tableView1 indexPathForRowAtPoint:rootViewPoint]; 
NSLog(@"%@",indexPath); 

Il suo lavoro perfettamente

+0

Questo è più appropriato invece di usare 'superView.superView' – Mrug

0

Nel caso in cui si utilizza una vista raccolta, è può utilizzare il metodo Yogesh, ma cambiare indexPathForRowAtPoint per indexPathForItemAtPoint come questo:

CGPoint center= [sender center]; 
CGPoint rootViewPoint = [[sender superview] convertPoint:center toView:_tableView1]; 
NSIndexPath *indexPath = [_tableView1 indexPathForRowAtPoint:rootViewPoint]; 
NSLog(@"%@",indexPath); 
Problemi correlati