iphone uitableview uibutton

iphone - UIButton en UITableViewCell



(7)

He usado un enfoque diferente, es un poco más fácil, espero que funcione para usted. Simplemente configure el estado resaltado del botón como falso dentro de los dos métodos de delegado anteriores:

-(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath { UIButton *btnAction = (UIButton *) [[tableView cellForRowAtIndexPath:indexPath] viewWithTag:3]; btnAction.highlighted = NO; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UIButton *btnAction = (UIButton *) [[tableView cellForRowAtIndexPath:indexPath] viewWithTag:3]; btnAction.highlighted = NO; }

Tengo un UIButton con una imagen dentro de una UITableViewCell. Cuando la celda se resalta, el botón también ingresa el estado resaltado (es decir, un tono más oscuro de la imagen), independientemente de si el usuario está haciendo clic dentro de los límites del botón o no.

No quiero esta funcionalidad: solo quiero que el botón se resalte cuando se hace clic en el botón, no cuando se hace clic en la celda completa.

Intenté configurar la imagen en el estado resaltado para que sea la misma que la imagen normal. Esto soluciona el problema, pero evita que el botón cambie de color cuando realmente está resaltado.

¿Alguna idea de cómo lograr el efecto deseado?


La sugerencia de codecafeine no funcionó para mí (iOS 8.3), pero me puso en el camino correcto. Lo modifiqué así (sin embargo, está en Swift):

override func setHighlighted(highlighted: Bool, animated: Bool) { var colorBefore = self.myButton.backgroundColor super.setHighlighted(highlighted, animated: animated) self.myButton.highlighted = false self.myButton.backgroundColor = colorBefore }


No he intentado esto, pero ¿podría agregar un objetivo / acción al botón para UIControlEventTouchDown que actualizó su imagen de estado resaltado a lo que deseaba, y luego otro objetivo / acción para UIControlEventTouchUpInside / UIControlEventTouchUpOutside / UIControlEventTouchCancel que restablece la imagen resaltada a coincide con la imagen de estado normal?


Para que funcione, tuve que usar las siguientes subclases en UITableViewCell. El objeto ''botón'' es el botón dentro de la celda personalizada:

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { [self.button setShowsTouchWhenHighlighted:NO]; [super setHighlighted:highlighted animated:animated]; self.button.highlighted = NO; [self.button setShowsTouchWhenHighlighted:YES]; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [self.button setShowsTouchWhenHighlighted:NO]; [super setSelected:selected animated:animated]; self.button.highlighted = NO; // Configure the view for the selected state [self.button setShowsTouchWhenHighlighted:YES]; }

Intercalar las distintas líneas puede provocar que se resalte el botón.


Un enfoque sería "anular la selección" o "desmarcar" el botón cuando se selecciona la celda de la vista de tabla:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [yourButton setHighlighted:NO]; // do something cool }


Una solución posible sería que configure el estilo de selección de celdas como none. En ese caso, cuando selecciona la celda, no se resaltará.

Esta es solo la posible solución. Puede ser que tengas otras cosas en mente.


Esto me estaba volviendo loco. Descubrí que debes anular setHighlighted:animated: y setSelected:animated:

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { [super setHighlighted:highlighted animated:animated]; self.yourButton.highlighted = NO; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; self.yourButton.selected = NO; // If you don''t set highlighted to NO in this method, // for some reason it''ll be highlighed while the // table cell selection animates out self.yourButton.highlighted = NO; }