打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
如何改变section header颜色?
http://ios.9tech.cn/news/2013/1128/38973.html


本文选自StackOverflow(简称:SOF)精选问答汇总系列文章之一,本系列文章将为读者分享国外最优质的精彩问与答,供读者学习和了解国外最新技术。本文将为读者讲解如何在UITableView里修改section header的颜色?

问题:

Ilya Suzdalnitski

怎样在UITableView里修改section header的颜色?

答案:

Alex Reynolds

希望这个从UITableViewDelegate协议里得到的方法可以对你有所帮助:

1
2
3
4
5
6
7
8
9
- (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;
}

使用任何你喜欢UIColor代替[UIColor redColor]。你可能还希望调整headerView的尺寸。

DoctorG

这是改变文本颜色的方法:

1
2
3
4
5
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];

whyoz

不要忘记从委托添加这段代码,否则在某些情况下视图将被切断或者出现在table后面,相对于视图/标签的高度。

1
2
3
4
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

Leszek ?arna

如果你想自定义header颜色,可以这样做:

1
[[UITableViewHeaderFooterView appearance] setTintColor:[UIColor redColor]];

这个方法在iOS 6.0.以上都很好用。

Maulik

这是在标题视图添加图片的方法:

1
2
3
4
5
6
7
8
9
10
11
- (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;
}

William Jockusch

如果你不想建立自定义视图,你也可以这样改变颜色(需要在iOS6里):

1
2
3
4
5
6
7
8
-(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;
    }
}

Dj S

这是常见的问题,我认为答案需要更新一下。

这个方法不涉及定义和创建自定义视图。在iOS 6以上,你可以通过以下方法轻松改变背景色和文本色:

1
2
3
- (void)tableView:(UITableView *)tableView
        willDisplayHeaderView:(UIView *)view
        forSection:(NSInteger)section

委托方法

例如:


1
2
3
4
5
6
7
8
9
10
11
12
13
- (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];
}

orbv

通过UITableViewHeaderFooterView设置背景色的方法已经被废弃了。请用contentView.backgroundColor代替。

原文链接:http://stackoverflow.com/questions/813068/uitableview-change-section-header-color

文章选自StackOverFlow社区,鉴于其内容对于开发者有所帮助,现将文章翻译于此,供大家参考及学习。9Tech将每日持续更新,读者可点击StackOverflow(简称:SOF)精选问答汇总,查看全部译文内容。同时,我们也招募志同道合的技术朋友共同翻译,造福大家!报名请发邮件至zhangqi_wj#cyou-inc.com。(#换成@

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
如何让 UITableView 的 headerView跟随 cell一起滚动
UITableView的 header的颜色及设置不悬浮停靠
TableView自定义header和footer
iOSTableView的代理函数
iOS开发 UITableView索引
iOS开发UI篇—UITableview控件基本使用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服