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]: 認識されないセレクターがインスタンス 0xecad20 に送信されました

これは、このラベルをスタイリングする非推奨のアプローチですか、それとも iOS8 のバグですか?

4

1 に答える 1

4

同じ問題がありました。以前の iOS バージョンでは、カスタム ヘッダー ビューがある場合、デリゲートはwillDisplayHeaderView:forSection:カスタム ビューのセクションに対して呼び出されず、型キャストは安全でした。どうやら、カスタムのヘッダーであっても、すべてのヘッダーに対してあなたに電話するようです。したがって、viewパラメーターは、実際の UITableViewHeaderFooterView ではなく、カスタム UIControl である可能性があります。デリゲートへの新しい 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 に答える