2010-04-26 8 views

Respuesta

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

He agregado esta declaración en el método didSelectRowAtIndexPath de UITableView, pero cuando toco en Button no está yendo en didSelectRowAtIndexPath. Si toco la celda/fila de UITableView fuera del botón, ingresa didSelectRowAtIndexPath. No entiendo por qué sucede esto ... ¿Cómo resolver esto? –

+0

Agregaría este objetivo justo después de crear el 'UIButton', y luego agregará el botón a la celda cuando se inicialice la celda. El método 'didSelectRowAtIndexPath' es para responder al tocar la celda (fila) en sí, no el botón. Esta acción de destino es para el botón en sí. ¿Esto es más claro? –

+0

Quiero crear UIButton en cada celda de UITabelView y cuando hago clic en ese UIButton, debe cambiar la imagen en UIButton. Entonces, ¿dónde crear UIButton para cada celda y código siguiente para que funcione para cada UIButton en cada celda de UITableView. [myCellButton addTarget: acción propia: @selector (myCellButtonPressed :) forControlEvents: UIControlEventTouchUpInside]; –

0

Si estás haciendo todo en el código ...

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

Pero sugeriría para usar una subclase UITableViewCell con delegación.

Cuestiones relacionadas