如何在iOS7风格上实现`viewForHeaderInSection`?

Dmi*_*try 22 iphone objective-c uitableview ipad ios7

如何实现(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)sectioniOS7风格的(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section作品?

PS我只需要更改标题的标题颜色UITableView并保存它的样式.

aum*_*are 52

在使用willDisplayHeaderView渲染之前,您可以在标准tableview标题内更改标签的颜色.也适用于iOS6/iOS7.

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    if([view isKindOfClass:[UITableViewHeaderFooterView class]]){

        UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *) view;
        tableViewHeaderFooterView.textLabel.textColor = [UIColor blueColor];
    }
}
Run Code Online (Sandbox Code Playgroud)

以供参考

UITableViewHeaderFooterView:https: //developer.apple.com/documentation/uikit/uitableviewheaderfooterview

UITableViewDelegate协议:https: //developer.apple.com/documentation/uikit/uitableviewdelegate/1614905-tableview


one*_*ray 8

如果您需要更改页眉和页脚的全局颜色,请使用appearance:

[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor redColor]];
Run Code Online (Sandbox Code Playgroud)


isa*_*aac 6

在视图控制器的生命周期的早期(例如-viewDidLoad),注册该类:

 [[self tableView] registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"headerFooterReuseIdentifier"];
Run Code Online (Sandbox Code Playgroud)

然后,在你的方法中:(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)sectiondeque a cell,按照你的意愿设置它,然后返回它:

 UIColor *titleColor = ... // Your color here.
 UITableViewHeaderFooterView *headerFoorterView = [[self tableView] dequeueReusableHeaderFooterViewWithIdentifier:@"headerFooterReuseIdentifier"];
 [[headerFooterView textLabel] setTextColor:titleColor];
 return headerFooterView;
Run Code Online (Sandbox Code Playgroud)

这就是你使用默认实现的方式.