PROBLEM IN FIXING TEXTFIELD INTO UILABEL

John Brown

Total Post:6

Points:42
Posted by  John Brown
 832  View(s)
Ratings:
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;

@end

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.

  1. Tarun Kumar

    Post:214

    Points:1642
    Re: Problem in fixing TextField into UILabel

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!