【问题标题】:textLabel on UITableViewHeaderFooterView no longer accessible in iOS8?UITableViewHeaderFooterView 上的 textLabel 在 iOS8 中不再可访问?
【发布时间】:2014-06-04 17:34:33
【问题描述】:
-(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 中的一个错误?

【问题讨论】:

  • 这个类的 API 没有改变,所以看起来不是这样。我通常会说这是您的代码的问题(似乎标题视图实际上只是UIView 而不是UITableViewHeaderFooterView)但如果这在早期版本中确实有效,那么我怀疑这是一个 iOS 8 错误.
  • @StevenOjo :我在我的应用程序中使用了相同的方法(使用 Xcode6 GM - iOS8 SDK 构建)并在具有 iOS8 GM 的 iPhone5 中运行,并且它的工作正常,没有任何崩溃。请尝试在真机上运行,​​可能是模拟器问题。
  • 我的应用程序中存在同样的问题。请参阅下面的答案!

标签: ios objective-c unrecognized-selector ios8


【解决方案1】:

我有同样的问题。在以前的 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.");
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多