Dj *_*j S 724
这是一个老问题,但我认为答案需要更新.
此方法不涉及定义和创建自己的自定义视图.在iOS 6及更高版本中,您可以通过定义来轻松更改背景颜色和文本颜色
-(void)tableView:(UITableView *)tableView
willDisplayHeaderView:(UIView *)view
forSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)
部分委托方法
例如:
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
// Background color
view.tintColor = [UIColor blackColor];
// Text Color
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[header.textLabel setTextColor:[UIColor whiteColor]];
// Another way to set the background color
// Note: does not preserve gradient effect of original header
// header.contentView.backgroundColor = [UIColor blackColor];
}
Run Code Online (Sandbox Code Playgroud)
取自我的帖子:https: //happyteamlabs.com/blog/ios-how-to-customize-table-view-header-and-footer-colors/
斯威夫特3/4
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
view.tintColor = UIColor.red
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.white
}
Run Code Online (Sandbox Code Playgroud)
Ale*_*lds 388
希望UITableViewDelegate协议中的这个方法可以帮助您入门:
Objective-C的:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
if (section == integerRepresentingYourSectionOfInterest)
[headerView setBackgroundColor:[UIColor redColor]];
else
[headerView setBackgroundColor:[UIColor clearColor]];
return headerView;
}
Run Code Online (Sandbox Code Playgroud)
迅速:
func tableView(_ tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView!
{
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 30))
if (section == integerRepresentingYourSectionOfInterest) {
headerView.backgroundColor = UIColor.redColor()
} else {
headerView.backgroundColor = UIColor.clearColor()
}
return headerView
}
Run Code Online (Sandbox Code Playgroud)
2017年更新:
斯威夫特3:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 30))
if (section == integerRepresentingYourSectionOfInterest) {
headerView.backgroundColor = UIColor.red
} else {
headerView.backgroundColor = UIColor.clear
}
return headerView
}
Run Code Online (Sandbox Code Playgroud)
替换[UIColor redColor]为UIColor您想要的任何一个.您可能还希望调整尺寸headerView.
Doc*_*orG 97
以下是更改文本颜色的方法.
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 18)] autorelease];
label.text = @"Section Header Text Here";
label.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.75];
label.backgroundColor = [UIColor clearColor];
[headerView addSubview:label];
Run Code Online (Sandbox Code Playgroud)
Les*_*rna 50
如果您想要具有自定义颜色的标题,可以执行此操作:
[[UITableViewHeaderFooterView appearance] setTintColor:[UIColor redColor]];
Run Code Online (Sandbox Code Playgroud)
这个解决方案从iOS 6.0开始运行良好.
Max*_*Max 31
以下解决方案适用于iOS 8+的Swift 1.2
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
// This changes the header background
view.tintColor = UIColor.blueColor()
// Gets the header view as a UITableViewHeaderFooterView and changes the text colour
var headerView: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
headerView.textLabel.textColor = UIColor.redColor()
}
Run Code Online (Sandbox Code Playgroud)
why*_*yoz 21
不要忘记从代理中添加这段代码,或者在某些情况下,相对于视图/标签的高度,您的视图将被截断或显示在表格后面.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}
Run Code Online (Sandbox Code Playgroud)
Wil*_*sch 18
如果您不想创建自定义视图,还可以更改颜色(需要iOS 6):
-(void) tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {
UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
UIView* content = castView.contentView;
UIColor* color = [UIColor colorWithWhite:0.85 alpha:1.]; // substitute your color here
content.backgroundColor = color;
}
}
Run Code Online (Sandbox Code Playgroud)
Roo*_*ahi 13
设置截面区域的背景和文本颜色:(感谢William Jockusch和Dj S)
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {
UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
castView.contentView.backgroundColor = [UIColor grayColor];
[castView.textLabel setTextColor:[UIColor grayColor]];
}
}
Run Code Online (Sandbox Code Playgroud)
Nii*_*tse 12
斯威夫特4
要更改UITableView部分的标题视图的背景颜色,文本标签颜色和字体,只需覆盖willDisplayHeaderView表视图,如下所示:
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
header.backgroundView?.backgroundColor = .white
header.textLabel?.textColor = .black
header.textLabel?.font = UIFont(name: "Helvetica-Bold", size: 14)
}
Run Code Online (Sandbox Code Playgroud)
这对我很有用; 希望它也对你有所帮助!
Mau*_*lik 10
以下是在标题视图中添加图像的方法:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
UIImageView *headerImage = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top-gery-bar.png"]] autorelease];
headerImage.frame = CGRectMake(0, 0, tableView.bounds.size.width, 30);
[headerView addSubview:headerImage];
return headerView;
}
Run Code Online (Sandbox Code Playgroud)
快速 5 +
在willDisplayHeaderView方法中
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
//For Header Background Color
view.tintColor = .black
// For Header Text Color
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = .white
}
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助你 :]
对于iOS8(Beta)和Swift,选择你想要的RGB颜色,试试这个:
override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {
var header :UITableViewHeaderFooterView = UITableViewHeaderFooterView()
header.contentView.backgroundColor = UIColor(red: 254.0/255.0, green: 190.0/255.0, blue: 127.0/255.0, alpha: 1)
return header
Run Code Online (Sandbox Code Playgroud)
}
("覆盖"是因为我在我的项目中使用UITableViewController而不是普通的UIViewController,但它不是强制更改节标题颜色)
您的标题文字仍会显示.请注意,您需要调整节标题高度.
祝好运.
小智 6
SWIFT 2
我能够通过添加模糊效果(这非常酷)成功更改了部分背景颜色.要轻松更改部分的背景颜色:
然后对于模糊效果,添加到代码:
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
// This is the blur effect
let blurEffect = UIBlurEffect(style: .Light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
// Gets the header view as a UITableViewHeaderFooterView and changes the text colour and adds above blur effect
let headerView: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
headerView.textLabel!.textColor = UIColor.darkGrayColor()
headerView.textLabel!.font = UIFont(name: "HelveticaNeue-Light", size: 13)
headerView.tintColor = .groupTableViewBackgroundColor()
headerView.backgroundView = blurEffectView
}
Run Code Online (Sandbox Code Playgroud)
Swift 4 让它变得非常简单。只需将其添加到您的班级并根据需要设置颜色。
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
view.backgroundColor = UIColor(red: 0.094, green: 0.239, blue: 0.424, alpha: 1.0)
}
Run Code Online (Sandbox Code Playgroud)
或者如果一个简单的颜色
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
view.backgroundColor = UIColor.white
}
Run Code Online (Sandbox Code Playgroud)
为 Swift 5 更新
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
view.tintColor = UIColor(red: 0.094, green: 0.239, blue: 0.424, alpha: 1.0)
}
Run Code Online (Sandbox Code Playgroud)
或者如果一个简单的颜色
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
view.tintColor = UIColor.white
}
Run Code Online (Sandbox Code Playgroud)
对我来说,在浪费了 2 个小时后,以上都不起作用,这就是解决方案。就我而言,它是自定义视图,但由于某种原因我无法从故事板和视图的 awakeFromNib 更改它。
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
header.contentView.backgroundColor = .white
}
Run Code Online (Sandbox Code Playgroud)
我知道它的答案,以防万一,在Swift中使用以下内容
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let tableViewWidth = self.tableView.bounds
let headerView = UIView(frame: CGRectMake(0, 0, tableViewWidth.size.width, self.tableView.sectionHeaderHeight))
headerView.backgroundColor = UIColor.greenColor()
return headerView
}
Run Code Online (Sandbox Code Playgroud)