2010-06-16 16 views
6

Tengo un UILabel para cada celda en cellForRowAtIndexPath.Acceso a la propiedad de etiqueta personalizada en didSelectRowAtIndexPath

UILabel *cellLabel = [[UILabel alloc] initWithFrame:frame]; 
cellLabel.text = myString; 

Quiero tener acceso a esa cadena "miCadena" en didSelectRowAtIndexPath usando indexpath.

NSString *anotherString = cell.textLabel.text; 

devuelve null.

Ahora, si en cellForRowAtIndexPath, hice algo como cell.textLabel.text = theString;, entonces didSelectRowAtIndexPath devuelve la celda adecuada.

Mi pregunta es, ¿cómo puedo acceder al texto en el UILabel que aplico a la celda, en didSelectRowAtIndexPath?

Además, el registro de la celda en didSelectRowAtIndexPath devuelve cell: <UITableViewCell: 0x5dcb9d0; frame = (0 44; 320 44); autoresize = W; layer = <CALayer: 0x5dbe670>>

Editar:

NSString *myString = [[results objectAtIndex:indexPath.row] valueForKey:@"name"]; 
//cell.textLabel.text = myString; 

CGFloat width = [UIScreen mainScreen].bounds.size.width - 50; 
CGFloat height = 20; 
CGRect frame = CGRectMake(10.0f, 10.0f, width, height); 

UILabel *cellLabel = [[UILabel alloc] initWithFrame:frame]; 
cellLabel.text = myString; 
cellLabel.textColor = [UIColor blackColor]; 
cellLabel.backgroundColor = [UIColor whiteColor]; 
cellLabel.textAlignment = UITextAlignmentLeft; 
cellLabel.font = [UIFont systemFontOfSize:14.0f]; 
[cell.contentView addSubview:cellLabel]; 
[cellLabel release]; 

return cell; 

Respuesta

17

En esta línea de código:

NSString *anotherString = cell.textLabel.text; 

¿Cómo estás obtención de la célula? ¿Es nulo? Además, el campo textLabel al que está accediendo es la etiqueta predeterminada en UITableViewCell y no la etiqueta que está agregando en -cellForRowAtIndexPath. Aquí es cómo se puede obtener la célula de -didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tv 
       didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [self tableView:tv cellForRowAtIndexPath:indexPath]; 

} 

El problema en este punto, sin embargo, es que no se puede acceder al UILabel por su nombre, sin embargo, se puede acceder a él si' he puesto una etiqueta. Por lo tanto, al crear su UILabel, establecer la etiqueta como esta:

UILabel *cellLabel = [[UILabel alloc] initWithFrame:frame]; 
cellLabel.text = myString; 
cellLabel.textColor = [UIColor blackColor]; 
cellLabel.backgroundColor = [UIColor whiteColor]; 
cellLabel.textAlignment = UITextAlignmentLeft; 
cellLabel.font = [UIFont systemFontOfSize:14.0f]; 

// Set the tag to any integer you want 
cellLabel.tag = 100; 

[cell.contentView addSubview:cellLabel]; 
[cellLabel release]; 

lo tanto, ahora se puede acceder al UILabel por etiqueta:

- (void)tableView:(UITableView *)tv 
      didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [self tableView:tv cellForRowAtIndexPath:indexPath]; 

    UILabel *label = (UILabel*)[cell viewWithTag:100]; 

    NSLog(@"Label Text: %@", [label text]); 
} 
+0

Enfoque muy interesante. Sin embargo, con esta implementación recibo una advertencia: tipos incompatibles de Objective-C inicializando 'struct UIView *', 'struct UILabel * esperado' en la línea "UILabel * label = [cell viewWithTag: 100];" –

+0

Simplemente escriba "cast it" a un UILabel *, p. UILabel * label = (UILabel *) [celda viewWithTag: 100]; Verifique la actualización de mi respuesta. –

+0

Nunca hubiera pensado en esto.Funcionó perfectamente. ¿Es esta la forma estándar de capturar texto de celda cuando se usa una etiqueta personalizada en cellForRowAtIndexPath? Hay mucha información en la red para crear celdas personalizadas, no tanto para acceder a ellas. Gracias por la ayuda. –

1

Se puede publicar el código que se asigna el cellLabel a la célula? Hiciste algo como esto: cell.textLabel = cellLabel?

Busque UITableViewCell para más detalles

+0

código solicitado ha sido publicado en "Editar:" –

+0

Mi El problema es que si configuro directamente el archivo textTextLabelText, entonces eso y mi UILabel personalizado se superponen. Que es un dolor completo –

+0

¿Pero la celda ya tiene una etiqueta de texto? ¿Realmente desea agregar otro UILabel como subvista? Mi idea es que puede establecer textLabel directamente utilizando cell.textLabel = cellLabel no haciendo [cell.contentView addSubView: cellLabel] – vodkhang

Cuestiones relacionadas