Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

textLabel on UITableViewHeaderFooterView no longer accessible in iOS8?

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
    UITableViewHeaderFooterView *headerIndexText = (UITableViewHeaderFooterView *)view;
    [headerIndexText.textLabel setTextColor:[UIColor whiteColor]];
}

The above code works fine on iOS6 and iOS7 and has been in production for a while. However, when running on iOS8 on the iPhone5S simulator, the application crashes with the following error:

-[UIView textLabel]: unrecognized selector sent to instance 0xeccad20

Is this a deprecated approach to styling this label, or is this a bug in iOS8?

like image 255
StevenOjo Avatar asked Oct 01 '22 15:10

StevenOjo


1 Answers

I had the same issue. In previous iOS versions, if you had a custom header view, your delegate was not called with willDisplayHeaderView:forSection: for sections with your custom view, and the typecast was safe. Now apparently they'll call you for every header, even your custom ones. Thus, the view parameter may be your custom UIControl, not an actual UITableViewHeaderFooterView. To filter out the new iOS8 calls to your delegate, protect it as follows:

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
     if([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
        UITableViewHeaderFooterView *headerIndexText = (UITableViewHeaderFooterView *)view;
        [headerIndexText.textLabel setTextColor:[UIColor whiteColor]];
    } else {
        NSLog(@"This is the new iOS case where the delegate gets called on a custom view.");
    }
}
like image 85
RobP Avatar answered Oct 13 '22 01:10

RobP