UITableView更改标题标题颜色

我正在使用InAppSettingsKit中的UITableView样式,并希望更改标题标题的颜色:

tableview的形象

Without titleText Field的标签应为白色。 如何才能做到这一点?

谢谢。

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

 cell.textLabel.backgroundColor = //Your color; cell.detailTextLabel.backgroundColor = //Your color; 

您可以参考以下内容获取更多详细说明,您可以在其中找到与您提到的相似的创建自定义分区表的详细示例 – http://undefinedvalue.com/2009/08/25/changing-background-color-and-section -header文本颜色分组的风格-的UITableView

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

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

 -(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger) 

委托方法。

例如:

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

取自我的post: https : //happyteamlabs.com/blog/ios-how-to-customize-table-view-header-and-footer-colors/

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

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

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

我花了几分钟将其“翻译”为Swift,但这里有一个与Swift 1.2(iOS 8)相同的工作。 确保在您的类中实现UITableViewDelegate

 // MARK: - VIEW METHODS override func viewDidLoad() { tableView.delegate = self } // MARK: - TABLEVIEW DELEGATE METHODS override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { let header = view as! UITableViewHeaderFooterView header.textLabel.textColor = UIColor.whiteColor() } 

我发现了这个: 如何在iPhone SDK的Grouped TableView中更改Section Headers的文本颜色? 它是一种享受,我把它与@rishi的答案链接中的一些代码相结合,它让我的生活变得更加轻松! 虽然我不得不稍微调整标签视图的协调,但它的工作就像一个魅力。