Ios 如何在titleForHeaderInSection方法中更改字体样式和背景色
在长时间阅读和检查代码之后,我很自豪拥有一个自定义的表视图,其中包含节和节标题,所有这些都是从核心数据对象填充的。现在我需要自定义部分标题和背景色。我已经看过了,但是在一个Ios 如何在titleForHeaderInSection方法中更改字体样式和背景色,ios,uitableview,Ios,Uitableview,在长时间阅读和检查代码之后,我很自豪拥有一个自定义的表视图,其中包含节和节标题,所有这些都是从核心数据对象填充的。现在我需要自定义部分标题和背景色。我已经看过了,但是在一个viewForHeaderInSection方法中。在我的标题中,是否不可能使用headerinsection方法? 这是我的方法: -(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
viewForHeaderInSection
方法中。在我的标题中,是否不可能使用headerinsection
方法?
这是我的方法:
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
id <NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections]objectAtIndex:section];
NSString *sectionname = [theSection name];
if ([sectionname isEqualToString:@"text 1"]){
return @"Today";
}
else if ([sectionname isEqualToString:@"text 2"]){
return @"Tomorrow";
}
if ([[self.fetchedResultsController sections]count]>0){
id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
return [sectionInfo name];
}
else{
return nil;
}
}
-(NSString*)表视图:(UITableView*)表视图标题标题标题部分:(NSInteger)部分{
id theSection=[[self.fetchedResultsController节]objectAtIndex:section];
NSString*sectionname=[theSection name];
如果([sectionname IsequalString:@“text 1”]){
返回"今日",;
}
else if([sectionname IsequalString:@“text 2”]){
返回"明天",;
}
如果([[self.fetchedResultsController节]计数]>0){
id sectionInfo=[[self.fetchedResultsController节]objectAtIndex:section];
返回[sectionInfo name];
}
否则{
返回零;
}
}
通过以下方式创建自定义标题视图:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *viewHeader = [UIView.alloc initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 28)];
UILabel *lblTitle =[UILabel.alloc initWithFrame:CGRectMake(6, 3, 136, 21)];
[lblTitle setFont:[UIFont fontWithName:@"HelveticaNeue" size:13]]; //Font style
[lblTitle setTextColor:[UIColor blackColor]];
[lblTitle setTextAlignment:NSTextAlignmentLeft];
[lblTitle setBackgroundColor:[UIColor clearColor]]; //Background
[viewHeader addSubview:lblTitle];
return viewHeader;
}
给这个标签任何文字;我建议为标题标题单独设置一个NSArray。我认为在
titleForHeaderInSection
中,您不能更改节视图属性。
添加到您的项目viewForHeaderInSection
,然后添加带有标签的自定义视图,例如。
请参见以下提示:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0)] autorelease];
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(0, 0,300, TABLE_HEADER_HEIGHT);
label.backgroundColor = [UIColor clearColor];
label.textColor = TABLE_HEADER_TITLE_COLOR;
label.font = [UIFont fontWithName:FONT_NAME size:14.f];
label.text = [self tableView:tableView titleForHeaderInSection:section];
if (section == 3) {
[headerView setBackgroundColor:[UIColor whiteColor]];
} else {
[headerView setBackgroundColor:[UIColor redColor]];
}
[headerView addSubview:label];
return headerView;
}
您可以根据节号设置标签的标题。下面是一个示例,它使用现有代码设置标题文本,但允许您使用UITableViewHeaderFooterView调整外观:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
static NSString *header = @"customHeader";
UITableViewHeaderFooterView *vHeader;
vHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:header];
if (!vHeader) {
vHeader = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:header];
vHeader.textLabel.backgroundColor = [UIColor redColor];
}
vHeader.textLabel.text = [self tableView:tableView titleForHeaderInSection:section];
return vHeader;
}
如果需要,您甚至可以将UITableViewHeaderFooterView
子类化,就像将UITableViewCell
子类化一样,进一步自定义外观。简单且优化
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
// Background color
view.tintColor = [UIColor whiteColor];//[UIColor colorWithRed:77.0/255.0 green:162.0/255.0 blue:217.0/255.0 alpha:1.0];
// Text Color
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[header.textLabel setTextColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"tableSection"]]];
}
迅速实施@codercat的回答:
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
view.tintColor = UIColor(red: 0.967, green: 0.985, blue: 0.998, alpha: 1) // this example is a light blue, but of course it also works with UIColor.lightGray
var header : UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
header.textLabel.textColor = .red
}
谢谢,但我需要TitleForHeaderInstruction方法来保留当前方法以创建节行和标题…具体来说,它可以解决您的问题ViewForHeaderInstruction方法:id theSection=[[self.fetchedResultsController sections]objectAtIndex:section];通过此获取的结果数组获取任何值并设置标签文本。您的意思是我可以在viewForHeaderInSection方法中使用titleForHeaderInSection中的相同代码行,然后添加您的建议以格式化文本和背景颜色?仅用于从NSFetchResultsSection获取获取的结果。。。。并从中设置标签标题。简单。你不应该在这里使用UIView,它是不可重用的。在我看来,我的代码一切都很好,UIView不应该是可重用的。什么样的错误?您不应该一对一地复制该代码,这是对您的自定义的提示,例如,表格标题高度和表格标题颜色由me值定义。这很有效,谢谢,我可以在每个部分标题上选择不同的背景颜色吗?当然可以!任何更改都应超出if(!vHeader)条件的范围。任何总是一样的东西都应该在里面。(就像表格单元格一样。)谢谢,我已经根据节值更改了背景颜色,效果不错,但现在我需要在标题旁边添加节中的行数作为标签,比如今天(6),明天(17)…我可以在这个方法中实现这个吗?这在您使用iOS 9时会被折旧。@AnkishJain
dequeueReusableHeaderFooterViewWithIdentifier:
在文档中似乎不推荐使用。你指的是什么?很聪明,但值得注意的是,这可能会在未来的iOS版本中崩溃,因为API不能保证视图
是UITableViewHeaderFooterView
。可能希望使用条件强制转换更安全。tableView(\uu:ViewForHeaderSection:)不应该是更好的解决方案吗?