2012-05-01 11 views
15

Tengo una tableViewCell personalizada. Quiero indicar el toque del usuario resaltando. El estilo de selección de celda es UITableViewCellSelectionStyleBlue. En el controlador principal configuré self.clearsSelectionOnViewWillAppear = YES.UITableViewCell. ¿Cómo destaco la celda durante el toque hacia abajo?

Debería estar listo. Nop. La selección aún se adhiere a la celda. Lo que quiero es indicación de selección solo para la duración del toque hacia abajo. La apariencia debe regresar inmediatamente a la apariencia no seleccionada en el retoque.

¿Cómo puedo hacer esto?

Saludos,
Doug

Respuesta

42
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

} 
+0

¡Tenemos un winna! Para la gente de casa, la firma del método real es deselectRowAtIndexPath: animated :. Dulce. Aclamaciones. – dugla

+0

No puedo creer que gaste tanto para resolver este problema de dolor ... Simplemente piensas que el toque del cacao lo hará en didHighlightItemAtIndexPath de cualquier forma gracias a @Dancreek –

0

sobrescribir - (void)setSelected:(BOOL)selected animate:(BOOL)animated sin llamar súper

+0

La célula sólo se selecciona después del retoque: la pregunta quiere cambiar el color al tocar hacia abajo. – Zorayr

-3

que tenía el mismo código de abajo cuestión.La perfectamente trabajado para mí.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

[[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:[UIColor blueColor]]; 
[table reloadData]; 
} 
+2

¿De verdad quieres volver a cargar la mesa en cada evento táctil? Es absolutamente incorrecto cómo hacer el tema. – surfrider

+0

Esto convierte la celda en azul, pero permanece así y solo funciona en touchUpInside. ¿Qué pasa con el aterrizaje solo? –

+0

La celda está marcada como seleccionada solo después del retoque: la pregunta quiere cambiar el color al tocar hacia abajo; Además, estoy de acuerdo con @surfrider, ¿por qué volverías a cargar datos? – Zorayr

0

Esto funciona:

Se pone el backgroundview con la frontera de la célula que parece seperator.Do no cambia la configuración por defecto en Tableview interfaz builder.Make Seguro UITableViewCellSelectionStyleNone no está ajustado a selectionstyle. Estoy pegando el código de trabajo. :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
static NSString *kCellIdentifier = @"PlayListCell"; 
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier]; 
} 
MPMediaPlaylist *playList = [playlistCollection objectAtIndex:indexPath.row]; 
cell.textLabel.text = playList.name; 
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
// cell.selectionStyle = UITableViewCellSelectionStyleNone; 
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d Songs",[playList.items count]]; 
MPMediaItemCollection *playListMediaCollection = [playlistCollection objectAtIndex:indexPath.row ]; 

cell.imageView.image =[UIImage imageWithCGImage:[self getImageForCollection:playListMediaCollection.items]]; 

// the main code which make it highlight 

UIView *bgColorView = [[UIView alloc] init]; 
bgColorView.backgroundColor = [UIColor colorWithRed:170.0f/255.0 green:170.0f/255.0 blue:170.0f/255.0 alpha:1.0f]; 
[bgColorView.layer setBorderColor:[UIColor blackColor].CGColor]; 
[bgColorView.layer setBorderWidth:1.0f]; 
[cell setSelectedBackgroundView:bgColorView]; 


return cell; 

}

9

Debajo ejemplo Swift utiliza didHighlightRowAtIndexPath para cambiar el color de fondo de la celda en contacto abajo y didUnhighlightRowAtIndexPath para restablecer el color - ver abajo:

// MARK: UITableViewDelegate 

func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) { 
    if let cell = tableView.cellForRowAtIndexPath(indexPath) { 
    cell.backgroundColor = UIColor.greenColor() 
    } 
} 

func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) { 
    if let cell = tableView.cellForRowAtIndexPath(indexPath) { 
    cell.backgroundColor = UIColor.blackColor() 
    } 
} 
+0

esto no funciona. El color no se restablece en el retoque –

+0

¿Se puede poner un punto de interrupción en 'didUnhighlightRowAtIndexPath:' y confirmar que se llama al retoque? Si puede confirmar eso, entonces el siguiente culpable podría ser su lógica de cambio de color. – Zorayr

+0

lo siento, me di cuenta de que mi problema era que tenía "retrasos toques de contenido" seleccionados en mi guión gráfico. –

Cuestiones relacionadas