2010-08-14 13 views
6

Obecnie mam przycisk zdefiniowane w komórce oraz sposób śledzić swoje działania UITouchDown jak pokazano:Komórka UITableView - pobierz IndexPath.row z przycisku?

- (void) clickedCallSign:(id)sender { 

    int index = [sender tag]; 
    NSLog(@"event triggered %@",index); 

} 

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    //Callsign button 
    UIButton *button; 

    CGRect rect = CGRectMake(TEXT_OFFSET_X, BORDER_WIDTH, LABEL_WIDTH, LABEL_HEIGHT); 
    button = [[UIButton alloc] initWithFrame:rect]; 
    cell.tag=[indexPath row]; 
    button.tag=[indexPath row]; 
    [button addTarget:self action:@selector(clickedCallSign:) forControlEvents:UIControlEventTouchDown]; 
    [button setBackgroundColor:[UIColor redColor]]; 
    [button setTitle:@"hello" forState:UIControlStateNormal]; 
    [cell.contentView addSubview:button]; 
    [button release]; 
} 

Jednak po kliknięciu komórki w symulatorze, komunikat debug konsola jest: „zdarzenie wywołane (null) "a moja aplikacja wkrótce się zawiesza.

Jak poprawnie uzyskać wartość indexPath.row w mojej metodzie clickedCallSign?

+0

lepiej widzieć rozwiązanie dla znalezienia indexPath z naciśniętym przyciskiem: http://stackoverflow.com/a/16270198/308315 – iwasrobbed

Odpowiedz

2

pierwsze, index jest int, więc NSLog musi wyglądać tak (należy zwrócić uwagę na %d):

NSLog(@"event triggered %d", index); 

(To możliwe że prowadzi to do katastrofy, ale jest również prawdopodobne, że dzieje się coś zupełnie innego, co powoduje niestabilność.)

+0

"% d" zrobił trik, aby uzyskać wartość indexPath.row do klikniętego CallSign :) aplikacja wciąż zawiesza się jednak .. z tym stosem śledzenia: - [ UIButton setText:]: nierozpoznany selektor wysłany do instancji 0x6877a10 2010-08-13 21: 48: 30.324 RF [8047: 207] *** Ter minating app ze względu na nieprzechwycony wyjątek "NSInvalidArgumentException", powód: "- [setText UIButton:]: nierozpoznany selektor wysłany do instancji 0x6877a10 ' – unicornherder

+0

Prawdopodobnie zbyt dużo miejsca zwalniasz. Zarządzanie pamięcią w wyświetlanym fragmencie kodu wygląda dobrze, więc coś jest nie tak. –

+1

faktycznie .. znalazłem rozwiązanie, którego potrzebowałem tutaj: http://developer.apple.com/iphone/library/samplecode/Accessory/Introduction/Intro.html Dzięki za pomoc. Bardzo to doceniam. – unicornherder

2

Znacznik jest dobry, dopóki nie masz obu sekcji i rzędów. Spróbuj innego sposobu, aby uzyskać ścieżkę indeksu:

- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath { 

    //... 

    [button addTarget:self action:@selector(clickedCallSign:withEvent:) forControlEvents:UIControlEventTouchDown]; 

    //... 

} 

// Get the index path of the cell, where the button was pressed 
- (NSIndexPath*)indexPathForEvent:(id)event 
{ 
    NSSet *touches = [event allTouches]; 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentTouchPosition = [touch locationInView:self.tableView]; 
    return [self.tableView indexPathForRowAtPoint:currentTouchPosition]; 
} 

- (IBAction)clickedCallSign:(id)sender withEvent:(UIEvent*)event 
{ 
    NSIndexPath* buttonIndexPath = [self indexPathForEvent:event]; 
} 
2

Jeśli nie chcesz korzystać z pole znacznika, które przycisk wywołać tę metodę:

- (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]; 
    } 
} 
Powiązane problemy