objective-c 如何在 iOS7 样式上实现`viewForHeaderInSection`?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18817841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement `viewForHeaderInSection` on iOS7 style?
提问by Dmitry
How to implement (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)sectionon iOS7 style like (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)sectionworks?
如何(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section在 iOS7 风格上实现类似(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section作品?
P.S. I just need to change a color of header's title of the UITableViewand save it's style.
PS我只需要更改标题标题的颜色UITableView并保存它的样式。
回答by aumansoftware
You can change the color a label inside a standard tableview header prior to rendering with willDisplayHeaderView. Will also work for both iOS6/iOS7.
在使用 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];
}
}
For Reference
以供参考
UITableViewHeaderFooterView: https://developer.apple.com/documentation/uikit/uitableviewheaderfooterview
UITableViewHeaderFooterView: https://developer.apple.com/documentation/uikit/uitableviewheaderfooterview
UITableViewDelegate Protocol: https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614905-tableview
UITableViewDelegate 协议:https: //developer.apple.com/documentation/uikit/uitableviewdelegate/1614905-tableview
回答by onegray
If you need to change global color for both header and footer, then use appearance:
如果您需要更改页眉和页脚的全局颜色,请使用appearance:
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor redColor]];
回答by isaac
Early in your view controller's lifecycle (eg, -viewDidLoad), register the class:
在您的视图控制器生命周期的早期(例如,-viewDidLoad),注册该类:
[[self tableView] registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"headerFooterReuseIdentifier"];
Then, in your method: (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)sectiondeque a cell, style it however you wish, and return it:
然后,在您的方法中:(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section对单元格进行 deque,根据需要对其进行样式设置,然后返回:
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.
这就是您使用默认实现的方式。
回答by Dmitry
Not perfect but solution:
不完美,但解决方案:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *label = [[UILabel alloc] init];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
if (<is_iOS7>) {
label.text = [[NSString stringWithFormat:@" %@", <title>] uppercaseString];
} else {
if (<is_iPad>) {
label.text = [NSString stringWithFormat:@" %@", <title>];
} else {
label.text = [NSString stringWithFormat:@" %@", <title>];
}
}
return label;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 38.f;
}
回答by Jeff Collier
And @aumansoftware's answer is also the best way to add content into the standard header such as a button. Here is an example in Swift:
而@aumansoftware 的回答也是将内容添加到标准标题(例如按钮)中的最佳方式。这是 Swift 中的一个示例:
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if (view.isKindOfClass(UITableViewHeaderFooterView)) {
let headerView = view as UITableViewHeaderFooterView
// Label
headerView.textLabel.textColor = UIColor.blueColor()
// New Button
let addPeopleButton: UIButton = UIButton.buttonWithType(.ContactAdd) as UIButton
addPeopleButton.addTarget(self, action: "showPeoplePicker:", forControlEvents: UIControlEvents.TouchUpInside)
addPeopleButton.setTranslatesAutoresizingMaskIntoConstraints(false)
headerView.addSubview(addPeopleButton)
let right: NSLayoutConstraint = NSLayoutConstraint(item: addPeopleButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: -12)
let vertical: NSLayoutConstraint = NSLayoutConstraint(item: addPeopleButton, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0)
headerView.addConstraints([right, vertical])
}
}
回答by Mundi
It works exactly as in iOS 6. Create a view (or just a label that covers the whole header) and style it according to your requirements.
它的工作原理与 iOS 6 完全一样。创建一个视图(或只是一个覆盖整个标题的标签)并根据您的要求设置样式。

