No se puede establecer el campo de texto en UILabel
-
06-07-2019 - |
Pregunta
He creado la clase UITableCellView
llamada NoteCell
. El encabezado define lo siguiente:
#import <UIKit/UIKit.h>
#import "Note.h"
@interface NoteCell : UITableViewCell {
Note *note;
UILabel *noteTextLabel;
}
@property (nonatomic, retain) UILabel *noteTextLabel;
- (Note *)note;
- (void)setNote:(Note *)newNote;
@end
En la implementación tengo el siguiente código para el método setNote:
:
- (void)setNote:(Note *)newNote {
note = newNote;
NSLog(@"Text Value of Note = %@", newNote.noteText);
self.noteTextLabel.text = newNote.noteText;
NSLog(@"Text Value of Note Text Label = %@", self.noteTextLabel.text);
[self setNeedsDisplay];
}
Esto no puede establecer el campo de texto de UILabel
y la salida de los mensajes de registro es:
2008-11-03 18:09:05.611 VisualNotes[5959:20b] Text Value of Note = Test Note 1
2008-11-03 18:09:05.619 VisualNotes[5959:20b] Text Value of Note Text Label = (null)
También he intentado establecer el campo de texto de UILabel
usando la siguiente sintaxis:
[self.noteTextLabel setText:newNote.noteText];
Esto no parece hacer la diferencia.
Cualquier ayuda sería muy apreciada.
Solución
¿Has configurado tu noteTextLabel en alguna parte? Lo que me parece a mí es que estás enviando mensajes a un objeto nulo. Cuando se crea su celda, noteTextLabel es nulo. Si nunca lo configura, básicamente está haciendo lo siguiente:
[nil setText: newNote.noteText];
Y cuando luego intentas acceder, estás haciendo esto:
[nil text];
Que devolverá nulo.
En su método -initWithFrame: reuseIdentifier:
, debe crear explícitamente su noteTextLabel y agregarlo como una subvista a la vista de contenido de su celda:
self.noteTextLabel = [[[UILabel alloc] initWithFrame: CGRectMake(0, 0, 200, 20)] autorelease];
[self.contentView addSubview: self.noteTextLabel];
Entonces esto debería funcionar.
Además, como nota estilística, haría que la propiedad
para noteTextLabel sea de solo lectura, ya que solo querrá acceder desde fuera de la clase, nunca la configure.