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