Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement `viewForHeaderInSection` on iOS7 style?

How to implement (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section on iOS7 style like (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section works?

P.S. I just need to change a color of header's title of the UITableView and save it's style.

like image 548
Dmitry Avatar asked Sep 15 '13 21:09

Dmitry


3 Answers

You can change the color a label inside a standard tableview header prior to rendering with willDisplayHeaderView. Will also work for both 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];
    }
}

For Reference

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

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

like image 178
aumansoftware Avatar answered Nov 10 '22 23:11

aumansoftware


If you need to change global color for both header and footer, then use appearance:

[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor redColor]];
like image 8
onegray Avatar answered Nov 11 '22 01:11

onegray


Early in your view controller's lifecycle (eg, -viewDidLoad), register the class:

 [[self tableView] registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"headerFooterReuseIdentifier"];

Then, in your method: (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section deque a cell, style it however you wish, and return it:

 UIColor *titleColor = ... // Your color here.
 UITableViewHeaderFooterView *headerFoorterView = [[self tableView] dequeueReusableHeaderFooterViewWithIdentifier:@"headerFooterReuseIdentifier"];
 [[headerFooterView textLabel] setTextColor:titleColor];
 return headerFooterView;

And that's how you use a default implementation.

like image 6
isaac Avatar answered Nov 10 '22 23:11

isaac