无法在UILabel上设置文本字段

时间:2008-11-03 18:15:08

标签: objective-c cocoa-touch uilabel

我创建了名为UITableCellView的{​​{1}}类。标题定义了以下内容:

NoteCell

在实现中,我有#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 方法的以下代码:

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]; } 的文本字段,日志消息的输出为:

UILabel

我还尝试使用以下语法设置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) 的文本字段:

UILabel

这似乎没有什么区别。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:10)

您是否在任何地方设置了noteTextLabel?这看起来对我来说就是你的消息是一个零对象。创建单元格时,noteTextLabel为nil。如果你从未进行过设置,那你基本上就是这样做了:

[nil setText: newNote.noteText];

当你以后尝试访问它时,你就是这样做的:

[nil text];

将返回nil。

-initWithFrame:reuseIdentifier:方法中,您需要明确创建noteTextLabel,并将其作为子视图添加到您单元格的内容视图中:

self.noteTextLabel = [[[UILabel alloc] initWithFrame: CGRectMake(0, 0, 200, 20)] autorelease];
[self.contentView addSubview: self.noteTextLabel];

然后这应该有用。

另外,作为一个风格笔记,我会为noteTextLabel只读property,因为你只想从课外访问它,从不设置它。

相关问题