2010-03-05 61 views
9

我有一个非常简单的问题(我希望如此)。如何将UITableview中的部分标题颜色从默认蓝色更改为黑色透明? 在此先感谢。在UITableview中更改节标题的颜色

+1

这是一个类似的问题:http://stackoverflow.com/questions/813068/uitableview-change-section-header -color – nylund 2011-12-01 13:46:39

回答

18

需要实现在的UITableViewDelegate协议这种方法:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 

这里是到documentation

链接...和做这样的事情(Sub在自己的颜色):

UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 22)] autorelease]; 
[sectionView setBackgroundColor:[UIColor blackColor]]; 
return sectionView; 

您也可以使用整数部分替代颜色或类似的东西。我认为这些部分的默认高度是22,但您可以随心所欲地制作它。这是你的问题的意思吗?希望这可以帮助。

+0

谢谢,我想在IB中会有某种触发器切换成黑色。但无论如何,谢谢。 – 2010-03-05 22:20:59

+1

但请缓存这些视图。 UIKit中存在一个错误,当tableView被滚动时会导致请求标题视图,所以这个方法将在滚动时调用每个像素偏移量。 22px确实是默认的高度。 – Joost 2010-03-05 22:25:13

+1

嗨,我明白这个方法被称为滚动发生时......我们如何缓存这个,以保存不必要的意见被创建等..?如果您可以发布评论或更新您的帖子,这将是非常有用的。谢谢 – Pavan 2012-08-29 14:20:09

0
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
    { 
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,tableView.bounds.size.width, 30)]; 
    if (section == 0) 
    [headerView setBackgroundColor:[UIColor redColor]]; 
    else 
    [headerView setBackgroundColor:[UIColor clearColor]]; 
    return headerView; 
    } 
20

这是一个老问题,但我认为答案需要更新。

此方法不涉及定义您自己的自定义视图。
在iOS 6及更高版本中,您可以通过定义代理方法来轻松更改背景颜色和文本颜色。

例如:

 
- (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]; 
} 

从我这里后摘自: https://happyteamlabs.com/blog/ios-how-to-customize-table-view-header-and-footer-colors/