Home > DeveloperSection > Forums > Unable to set text field on UILabel
Jessica James
Jessica James

Total Post:16

Posted on    September-08-2015 11:51 PM

 iPhone iPhone  Ios  Objective C 

 1 Reply(s)
 681  View(s)
Rate this:

I have created UITableCellView class called MyCell. The header defines the following:

#import< UIKit/UIKit.h>

#import "MyNote.h"

@interface MyCell : UITableViewCell {

    MyNote *note;

    UILabel *noteTextLabel; 


@property (nonatomic, retain) UILabel *noteTextLabel;

- (MyNote *)note;

- (void)setNote:(MyNote *)newNote;



In the implementation I have the following code for the setNote: method:

- (void)setNote:(MyNote *)newNote {

    note = newNote;

    NSLog(@"Text Value of MyNote = %@", newNote.noteText);

    self.noteTextLabel.text = newNote.noteText;

    NSLog(@"Text Value of MyNote Text Label = %@", self.noteTextLabel.text);

    [self setNeedsDisplay];


This fails to set the text field of the UILabel and the output of the log messages is:

2008-11-03 18:09:05.611 VisualNotes[5959:20b] Text Value of MyNote = Test Note 1 

2008-11-03 18:09:05.619 VisualNotes[5959:20b] Text Value of MyNote Text Label = (null)


I have also tried to set the text field of UILabel using the following syntax:

[self.noteTextLabel setText:newNote.noteText];

This does not seem to make a difference.

Jennifer Morgan
Jennifer Morgan

Total Post:13

Posted on    September-09-2015 11:34 PM

Have you set up your noteTextLabel anywhere? What this looks like to me is that you're messaging a nil object. When you cell is created, noteTextLabel is nil. If you never set it up, you're basically doing the following: 

[nil setText: newNote.noteText];

And when you later try to access it, you're doing this:

[nil text];

Which will return nil.

In your -initWithFrame:reuseIdentifier: method, you need to explicitly create your noteTextLabel, and add it as a subview to your cell's content view:

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

[self.contentView addSubview: self.noteTextLabel];

Then this should work.

Also, as a stylistic note, I would make the property for noteTextLabel readonly, since you're only going to want to access it from outside the class, never set it.

Don't want to miss updates? Please click the below button!

Follow MindStick