我在InAppSettingsKit中设置 UITableView 的样式,并希望更改标题标题的颜色:
标签应该是白色的Without title
。Text Field
如何才能做到这一点?
谢谢。
这是一个老问题,但我认为答案需要更新。
此方法不涉及定义和创建您自己的自定义视图。在 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];
}
取自我在这里的帖子:https ://happyteamlabs.com/blog/ios-how-to-customize-table-view-header-and-footer-colors/
斯威夫特 5.0:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let header = view as? UITableViewHeaderFooterView {
header.textLabel?.textColor = .white
}
}
实现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;
}
请记住将标签框架设置为足够高以隔开各个部分。您可能希望将标签嵌入到更大的 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()
}
我发现了这个: How to change text color for Section Headers in a Grouped TableView in iPhone SDK? 这是一种享受,我将它与@rishi 答案中的链接中的一些代码结合起来,它让我的生活变得更加轻松!虽然我不得不稍微调整一下标签视图的协调,但它就像一个魅力。
斯威夫特 4 版本:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
guard let headerView = view as? UITableViewHeaderFooterView else { return }
headerView.textLabel?.textColor = .red // any color
}
您可以在表格单元格创建方法中尝试以下代码行 -
cell.textLabel.backgroundColor = //Your color;
cell.detailTextLabel.backgroundColor = //Your color;
您可以参考以下内容以获取更多详细说明,您可以在其中找到创建自定义分区表的详细示例,类似于您提到的内容 - http://undefinedvalue.com/2009/08/25/changeing-background-color-and-section -header-text-color-grouped-style-uitableview