Abhinav
Abhinav

Reputation: 38162

Crash while drawing a custom table view section header view

I am drawing a custom table section header view and passing it in "tableView:viewForHeaderInSection:" method. I am getting a crash while table view try to draw itself on the screen:

What could be the reason for this :-(?

Crash log says:

Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan 10]'
    *** Call stack at first throw:
0   CoreFoundation                      0x3611a64f __exceptionPreprocess + 114
        1   libobjc.A.dylib                     0x30638c5d objc_exception_throw + 24
        2   CoreFoundation                      0x3611a491 +[NSException raise:format:arguments:] + 68
        3   CoreFoundation                      0x3611a4cb +[NSException raise:format:] + 34
        4   QuartzCore                          0x30fd061d _ZL18CALayerSetPositionP7CALayerRKN2CA4Vec2IdEEb + 140
        5   QuartzCore                          0x30fd058b -[CALayer setPosition:] + 38
        6   QuartzCore                          0x30fd04d7 -[CALayer setFrame:] + 390
        7   UIKit                               0x32e1f455 -[UIView(Geometry) setFrame:] + 188
        8   UIKit                               0x32e200fb -[UILabel setFrame:] + 210
        9   MyApp                        0x003d4ed3 -[MyCustomView layoutSubviews] + 210

Here is the code for my layout subviews:

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat anEdgePadding = 28.0f;
    CGFloat aTitleLabelWidth = [self.titleLabel.text sizeWithFont:[UIFont boldSystemFontOfSize:kFontSize16]].width; 
    CGFloat aSubTitleLabelWidth = [self.subTitleLabel.text sizeWithFont:[UIFont boldSystemFontOfSize:kFontSize10]].width;
    self.titleLabel.frame = CGRectMake(myLeftRightMargin, -1, aTitleLabelWidth, self.bounds.size.height);
    self.subTitleLabel.frame = CGRectMake(self.bounds.size.width - aSubTitleLabelWidth - anEdgePadding, 0, aSubTitleLabelWidth, self.bounds.size.height);
}

Upvotes: 0

Views: 264

Answers (1)

Geekswordsman
Geekswordsman

Reputation: 1307

Its as the error states, one of your labels is setting either it's x or y origin position to something that is not a number (NaN).

You can find out which one by putting:

NSLog(@"TitleLabel: %p", self.titleLabel);
NSLog(@"subTitleLabel: %p", self.subTitleLabel);

Then compare the pointer values (0x32e200fb) with the crash log. The one that matches up is getting a bad value.

On a quick look, the titleLabel is using a constant (myLeftRightMargin) so I'd immediately look into that one. Have you made sure this is defined as a numeric primative?

Upvotes: 1

Related Questions