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.
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
If you need to change global color for both header and footer, then use appearance
:
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor redColor]];
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With