UITableView更改标题标题颜色

Iñi*_*tia 14 iphone uitableview ios inappsettingskit

我在InAppSettingsKit中设置了UITableView的样式,并希望更改标题标题的颜色:

tableview的形象

标签Without titleText Field应该是白色.如何才能做到这一点?

谢谢.

Dj *_*j S 62

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

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

-(void)tableView:(UITableView *)tableView 
    willDisplayHeaderView:(UIView *)view 
    forSection:(NSInteger)
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/

  • 顺便说一句,顺便说一句,这种方法在iOS 7中仍能正常运行. (2认同)

Nic*_*ood 5

tableView:viewForHeaderInSection:在tableViewController中实现该方法.这将允许您为标题提供自己的视图,其中可以包含具有您想要的任何格式的UILabel,例如

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *customLabel = [[UILabel alloc] init];
    customLabel.text = [self tableView:tableView titleForHeaderInSection:section];
    return customLabel;
}
Run Code Online (Sandbox Code Playgroud)

请记住将标签框架设置得足够高,以便将这些部分分开.您可能希望将标签嵌入到更大的UIView中并返回它以简化定位(例如,如果您想要增加标签上的左边距).

  • 我假设他正在使用ARC. (2认同)

ris*_*shi 0

您可以在表格单元格创建方法中尝试以下代码行 -

cell.textLabel.backgroundColor = //Your color;
cell.detailTextLabel.backgroundColor = //Your color;
Run Code Online (Sandbox Code Playgroud)

您可以参考以下内容以获取更详细的描述,其中您可以找到创建与您提到的类似的自定义分段表的详细示例 - http://undefinedvalue.com/2009/08/25/changing-background-color-and-section -标题文本颜色分组样式uitableview