-(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?
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.");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With