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

上面的代码在iOS6iOS7上运行良好,并且已经投入生产了一段时间。但是,在iPhone5S 模拟器上的iOS8上运行时,应用程序崩溃并出现以下错误:

-[UIView textLabel]:无法识别的选择器发送到实例 0xeccad20

这是为这个标签设置样式的一种已弃用的方法,还是 iOS8 中的一个错误?

4

1 回答 1

4

我遇到过同样的问题。在以前的 iOS 版本中,如果您有自定义标题视图,则不会willDisplayHeaderView:forSection:为具有自定义视图的部分调用您的委托,并且类型转换是安全的。现在显然他们会为每个标题调用您,甚至是您的自定义标题。因此,该view参数可能是您的自定义 UIControl,而不是实际的 UITableViewHeaderFooterView。要过滤掉对您的委托的新 iOS8 调用,请按如下方式保护它:

-(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.");
    }
}
于 2014-09-18T18:18:53.790 回答