2008-11-03 36 views
3

我创建了UITableCellView类为NoteCell。头定义了下面的:无法在UILabel上设置文本字段

#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文本字段:

[self.noteTextLabel setText:newNote.noteText]; 

这似乎没有什么区别。

任何帮助将不胜感激。

回答

10

您是否在任何地方设置了您的noteTextLabel?这对我来说看起来就是你传递了一个零对象。当您创建单元格时,noteTextLabel为零。如果你从来没有设置它,你基本上执行以下操作:

[nil setText: newNote.noteText]; 

,并在以后尝试访问它,你这样做是:

[nil text]; 

将返回零。

在你-initWithFrame:reuseIdentifier:方法,你需要明确创建noteTextLabel,并将其添加为一个子视图到您的内容观点:

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

那么这应该工作。

此外,作为一种风格的笔记,我只会将noteTextLabel作为只读property,因为您只想从课程外部访问它,从未设置它。

+1

感谢您的快速响应,它立即解决了问题。我不能相信我没有发现这一点,我一直在盯着这个小时,并开始密码盲。 – lucasweb 2008-11-03 18:37:27