2010-12-23 11 views
10

El código de abajo parece tener ningún efecto. Quiero que sea highlighed de la misma forma en que se pone de manifiesto al tocar en una filaCómo resaltar una fila en una UITableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ... 
    [cell.textLabel setHighlighted:YES]; 


    return cell; 
} 
+0

este [mensaje] (http://stackoverflow.com/questions/1845773/how-to-initially-select-a-row-in-uitableview) podría ayudar. – petert

Respuesta

27

Esta línea se encargará de volver a pintar la celda, etiquetas y accesorios para usted:

[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; 
1

tiene dos opciones . O bien utilizar la selección de la vista de tabla:

[tableView selectRowAtIndexPath: indexPath animated: YES scrollPosition: UITableViewScrollPositionNone]; 

O cambiar el color de fondo de la celda de la tabla en la vista de tabla delegado:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    cell.backgroundColor = ... 
} 
15
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection: 0]; 

[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; 

suele funcionar

Si está usando constructor de interfaces para construir su interfaz, una cosa es para comprobar que se haya especificado el punto de vista del controlador de vista de tabla. Si en realidad no se especifica el punto de vista, este mensaje de selección puede ir a ninguna parte.

(especifique la vista seleccionando el controlador de vista de tabla en la ventana del inspector del constructor de interfaz y arrastrando la salida para "ver" a la vista de tabla en la que desea hacer la selección).

2

Puede utilizar

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ... 
    [cell.textLabel setHighlighted:YES]; 


    return cell; 
} 
3
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath { 
    return YES; 
} 

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath { 
    // do something here 
} 

Prueba esto :)

+0

Solo disponible en iOS 6 http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html –

21

Como otros han señalado, se puede seleccionar una celda mediante programación utilizando este método:

[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; 

Sin embargo, debe tener cuidado de no para llamar a este método demasiado pronto.

La primera vez que el controlador de vista que contiene dicho tableView es initialized, lo más temprano que debe llamar a este método está dentro viewDidAppear:.

Por lo tanto, usted debe hacer algo como esto:

- (void)viewDidAppear:(BOOL)animated 
{ 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; // set to whatever you want to be selected first 
    [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; 
} 

Si intenta poner esta llamada en viewDidLoad, viewWillAppear:, o cualquier otra llamada temprana vista del ciclo de vida, es probable que no funcionan o tienen resultados extraños (tales como desplazarse a la posición correcta pero no seleccionar la celda).

+0

En iOS 9 parece funcionar desde 'viewWillApear' y' viewDidAppear 'y no de 'viewDidLoad'. – Adrian

Cuestiones relacionadas