Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a custom cell in a UITableView. I programatically created a label with a height of 200 and a width of 50. When I do an NSLog in the customCell.m of the label's width and height, it gives me w: 50 and h: 200. But when I do an NSLog in mainViewController.m it gives me 0 for the height and width.

Not sure why it does that. I need to get the real height of the label in the mainViewController.m

Here's my code:

Objective-C
customCell.m

- (void)awakeFromNib {

    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
    [self.label setBackgroundColor:[UIColor yellowColor]];
    [self.label setText:@"Hello"];
    [self.myView addSubview:self.label];

    CGRect myFrame = self.myView.frame;
    myFrame.size.height = self.label.frame.size.height;

    self.myView.frame = myFrame;
    NSLog(@"%f", self.label.frame.size.height);  // Results: 200.0000
}
mainViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    customCell *cellVC = [[cutsomCell alloc] init];

    NSLog(@"%f, %f", cellVC.label.frame.size.height, cellVC.frame.size.height); // Results: 0.0000, 44.0000
}

I set myView to 0 in the storyBoard.
Posted

1 solution

in your code in the awakeNib you set the size of the label. In your viewDidload it wont get called by the new instance.

PS: customCell is a bad class name
 
Share this answer
 
Comments
Member 11360293 2-Feb-15 16:39pm    
So how do I call it?
KarstenK 3-Feb-15 4:39am    
overwrite the init function or call initWithFrame

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900