在UITableview中更改节标题的颜色

Sim*_* D. 10 iphone objective-c uitableview sectionheader

我有一个非常简单的简单问题(我希望如此).如何将UITableview中的节标题颜色从默认蓝色更改为黑色透明?提前致谢.

Dj *_*j S 21

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

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

- (void)tableView:(UITableView *)tableView 
        willDisplayHeaderView:(UIView *)view 
        forSection:(NSInteger)section
委托方法.

例如:

- (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/


Rya*_*tti 18

您需要在UITableViewDelegate协议中实现此方法:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)

这是文档的链接

...并做这样的事情(你自己的颜色):

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

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