2011-04-30 14 views
58

Tengo una vista de tabla y quiero saber cómo puedo cambiar el color del texto de la fila seleccionada, por ejemplo, a Rojo? He intentado por este código:UITableViewCell cambio de color del texto de la fila seleccionada

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell= [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease]; 

    cell.text = [localArray objectAtIndex:indexPath.row]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    cityName = [localArray objectAtIndex:indexPath.row]; 

    UITableViewCell* theCell = [tableView cellForRowAtIndexPath:indexPath]; 
    theCell.textColor = [UIColor redColor]; 
    //theCell.textLabel.textColor = [UIColor redColor]; 

    [tableView deselectRowAtIndexPath:indexPath animated:NO]; 
} 

(1) Cuando selecciono cualquier fila a continuación, el color del texto cambia a la roja, pero cuando selecciono el texto de otra fila a continuación, previamente seleccionada permanece en rojo. Como puedo resolver esto ?

(2) Cuando me desplazo por la tabla, el color del texto cambia al color negro ¿cómo solucionarlo?

Gracias ..

Respuesta

193

Hagan esto en tableView:cellForRowAtIndexPath::

cell.textLabel.highlightedTextColor = [UIColor redColor]; 

(Y no use más cell.text = ... Se ha desaprobado por casi 2 años ahora utilizan cell.textLabel.text = ... lugar...)


Como Raphael Oliveira mencionado en los comentarios, si el estilo de selección de su celda es igual a UITableViewCellSelectionStyleNone esto no funcionará. Revise Storyboard también para el estilo de selección.

+0

¡el color debe ser rojo hasta que seleccione otra fila! – Maulik

+0

ya cierto ... ¡el color azul también permanece! – Maulik

+0

Oye, funciona bien, gracias. –

4

Si desea cambiar el color del texto solamente, sin cambiar el color de fondo de la celda. puede utilizar este código en this.Write en el método cellForRowAtIndexPath

UIView *selectionColor = [[UIView alloc] init]; 
selectionColor.backgroundColor = [UIColor clearColor]; 
cell.selectedBackgroundView = selectionColor; 
cell.textLabel.highlightedTextColor = [UIColor redColor]; 
+0

Estoy usando este método, pero en la configuración de iOS 10, el color seleccionado Vista de fondo hace que los separadores de tabla desaparezcan en la selección. Todavía estoy trabajando en una buena solución para este efecto secundario. – Shackleford

0

que tenía el mismo problema, intente esto!

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

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    for (id object in cell.superview.subviews) { 
     if ([object isKindOfClass:[UITableViewCell class]]) { 
      UITableViewCell *cellNotSelected = (UITableViewCell*)object; 
      cellNotSelected.textLabel.textColor = [UIColor blackColor]; 
     } 
    } 

    cell.textLabel.textColor = [UIColor redColor]; 

    [tableView deselectRowAtIndexPath:indexPath animated:NO]; 
} 

Esa puede ser la solución a su (y la mía) problema.

0

Si ya está haciendo una subclase en UITableViewCell, es más fácil/más limpio establecer los colores en el método awakeFromNib (suponiendo que esté instanciando desde un guión gráfico o xib).

@implementation MySubclassTableViewCell 

- (void)awakeFromNib { 
    [super awakeFromNib]; 
    self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame]; 
    self.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:0.1 green:0.308 blue:0.173 alpha:0.6]; 
    self.customLabel.highlightedTextColor = [UIColor whiteColor]; 
} 

@end 
Cuestiones relacionadas