ios objective-c iphone uitableview

ios - UITableViewCell cambio de color de texto seleccionado de la fila



objective-c iphone (4)

Tengo una vista de tabla y quiero saber cómo puedo cambiar el color del texto de la fila seleccionada, por ejemplo, en rojo. Intenté con este código:

- (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 una fila, el color del texto cambia a rojo, pero cuando selecciono otro, el texto de la fila seleccionada anteriormente permanece en rojo. Como puedo resolver esto ?

(2) Cuando desplazo el color del texto de la tabla, ¿cambio al color negro cómo resolver esto?

Gracias..


Haga esto en tableView:cellForRowAtIndexPath: ::

cell.textLabel.highlightedTextColor = [UIColor redColor];

(Y no use cell.text = ... más. Ha estado en desuso por casi 2 años. Use cell.textLabel.text = ... lugar).

Como mencionó Raphael Oliveira 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.


Si solo desea cambiar el color del texto, sin cambiar el color de fondo de la celda. puede usar esto. Escriba este código en el método cellForRowAtIndexPath

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


Si ya está subclasificando 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


Tuve el mismo problema, ¡prueba 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 problema (y el mío).