我创建了 UITableCellView 类,名为 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 的文本字段:

[self.noteTextLabel setText:newNote.noteText];

这似乎没有什么区别。

非常感谢任何帮助。

有帮助吗?

解决方案

您是否在任何地方设置了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 readonly创建属性,因为你只想从课外访问它,从不设置它。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top