我正在处理iOS 5,表格中有动态生成的单元格(2个部分,每个部分有3行),每个部分都有一个标题,也是使用titleForHeaderInSection
调用动态生成的。
此外,我还将一张图片设置为表格的背景,导致默认颜色的部分标题难以阅读。我无法通过故事板界面或编程方式更改部分标题的颜色(或阴影颜色
、字体
、文本大小
等)!请帮帮我!
我正在处理iOS 5,表格中有动态生成的单元格(2个部分,每个部分有3行),每个部分都有一个标题,也是使用titleForHeaderInSection
调用动态生成的。
此外,我还将一张图片设置为表格的背景,导致默认颜色的部分标题难以阅读。我无法通过故事板界面或编程方式更改部分标题的颜色(或阴影颜色
、字体
、文本大小
等)!请帮帮我!
这也适用于 iOS5+。它适用于表视图中的所有部分头和脚,并满足我的需求。
- (void)viewDidLoad
{
[super viewDidLoad];
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setFont:[UIFont fontWithName:@"MyFont" size:8]];
}
丹
您可以通过使用方法tableView:viewForHeaderInSection:
创建自己的视图,以修改段头的字体大小、颜色等。
如果你只需要进行一些简单的修改,例如改变字体或颜色:
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *)view;
tableViewHeaderFooterView.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:12.0f];
tableViewHeaderFooterView.textLabel.textColor = [UIColor colorWithRed:0.27f green:0.27f blue:0.27f alpha:1.0f];
tableViewHeaderFooterView.contentView.backgroundColor = [UIColor colorWithRed:0.87f green:0.87f blue:0.87f alpha:1.0f];
}
UITableViewHeaderFooterView
类实现了可重用的视图,可以放置在表格段的顶部或底部。您可以使用标题和页脚来显示该部分的其他信息。[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setColor:[UIColor whiteColor]];
setTextColor
,它在iOS 7中确实有效。但是,如果你想自定义视图而不仅仅是交换一些颜色,那么使用一个全新的视图 - 就像被接受的答案建议的那样 - 可能是最好的方法。 - Habizzle非常类似于用于的技术
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
您可以创建一个由您提供的单元格原型的实例。如果您的单元格包括标签的出口,您可以在返回之前设置它:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger) section
{
SessionTableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:@"SessionSectionHeader"];
if (cell == nil) {
cell = [[SessionTableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"SessionSectionHeader"];
}
cell.myLabel.text = myTitles[section];
return cell;
}
tableView:viewForHeaderInSection:
。谢谢! - Eric BakerappearanceWhenContainedInInstancesOfClasses(_:)
获取UIAppearance
实例。例如,UILabel.appearanceWhenContainedInInstancesOfClasses([UITableViewHeaderFooterView.self]).textColor = UIColor.whiteColor()
。 - Isaac Overacker