Home > DeveloperSection > Forums > Problem in fixing TextField into UILabel
John Brown
John Brown

Total Post:6

Posted on    October-06-2015 12:35 AM

 iPhone iPhone  Ios  Objective C 

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

I was creating an app using UITableView and the class name is CellDemo, so I need to create UITableCellView class, the header definition is here:

#import <UIKit/UIKit.h>

#import "Book.h"

@interface CellDemo : UITableViewCell {

    Book *book;

    UILabel *bookTextLabel; 


@property (nonatomic, retain) UILabel *bookTextLabel;

- (Book *)book;

- (void)setBook:(Book *)newBook;


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

- (void)setBook:(Book *)newBook {

    book = newBook;

    NSLog(@"Text Value of Book = %@", newBook.bookText);

    self.bookTextLabel.text = newBook.bookText;

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

    [self setNeedsDisplay];


After executing the code it fails and the output of log message is here:

2015-09-09 18:09:05.611 VisualBooks[5959:20b] Text Value of Book = Test Book 1 

2015-09-09 18:09:05.619 VisualBooks[5959:20b] Text Value of Book Text Label = (null)

 And I also try to set the text field into UILabel using this code:

[self.bookTextLabel setText:newBook.bookText];


This also generates same type error.

Please help me.

Tarun Kumar

Total Post:210

Posted on    October-06-2015 11:16 PM

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

[nil setText: newBook.bookText];

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 bookTextLabel, and add it as a subview to your cell's content view:

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

[self.contentView addSubview: self.bookTextLabel];

Then this should work.

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

Modified On Oct-06-2015 11:17:17 PM

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

Follow MindStick