2010-04-26 18 views

Odpowiedz

19
[myCellButton addTarget:self action:@selector(myCellButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
+0

Dodałem to stwierdzenie w metodzie didSelectRowAtIndexPath dla UITableView, ale po dotknięciu przycisku nie wchodzi ono do didSelectRowAtIndexPath. Jeśli dotknę komórki/wiersza opcji UITableView poza przyciskiem, nastąpi przejście do wnętrza didSelectRowAtIndexPath. Nie rozumiem, dlaczego tak się dzieje? ....... Jak rozwiązać ten problem? –

+0

Dodaj ten cel tuż po utworzeniu 'UIButton', a następnie dodaj przycisk do komórki, gdy komórka jest inicjowana. Metoda 'didSelectRowAtIndexPath' służy do odpytywania samej komórki (wiersza), a nie przycisku. Ta docelowa akcja dotyczy samego przycisku. Czy to jest jaśniejsze? –

+0

Chcę utworzyć UIButton na każdej komórce UITabelView i kiedy kliknę na UIButton powinien zmienić obraz na UIButton. Więc gdzie utworzyć UIButton dla każdej komórki i następującego kodu, aby działał dla każdego UIButton na każdej komórce UITableView. [myCellButton addTarget: self action: @selector (myCellButtonPressed :) forControlEvents: UIControlEventTouchUpInside]; –

0

Jeśli robisz to wszystko w kodzie ...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];   
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [button setTitle:@"Button" forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(cellButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
     [button sizeToFit]; 
     [cell.contentView addSubview:button];   
    } 

    ... 

    return cell; 
} 

...

- (IBAction)cellButtonPressed:(id)sender 
{ 
    [sender setImage:[UIImage imageNamed:@"newImage"] forState:UIControlStateNormal]; 
} 

Ale sugerowałbym używać podklasy UITableViewCell z delegacją.

Powiązane problemy