Não é possível definir campo de texto em UILabel
-
06-07-2019 - |
Pergunta
Eu criei classe UITableCellView
chamado NoteCell
. O cabeçalho define o seguinte:
#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
Na implementação Eu tenho o seguinte código para o 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];
}
Esta falha para definir o campo de texto da UILabel
ea saída das mensagens de log é:
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)
Eu também tentei para definir o campo de texto de UILabel
usando a seguinte sintaxe:
[self.noteTextLabel setText:newNote.noteText];
Este não parece fazer a diferença.
Qualquer ajuda seria muito apreciada.
Solução
Você configurar seu lugar noteTextLabel? Que isso parece para mim é que você está mensagens um objeto nulo. Quando celular é criado, noteTextLabel é nulo. Se você nunca configurá-lo, você está fazendo basicamente o seguinte:
[nil setText: newNote.noteText];
E quando você mais tarde tentar acessá-lo, você está fazendo isso:
[nil text];
que irá retornar nulo.
No seu método de -initWithFrame:reuseIdentifier:
, você precisa criar explicitamente seu noteTextLabel, e adicioná-lo como um subview para exibição de conteúdo do seu celular:
self.noteTextLabel = [[[UILabel alloc] initWithFrame: CGRectMake(0, 0, 200, 20)] autorelease];
[self.contentView addSubview: self.noteTextLabel];
Então isso deve funcionar.
Além disso, como uma nota de estilo, eu faria o property
para noteTextLabel somente leitura, uma vez que você só vai querer acessá-lo de fora da classe, não configurá-lo.