如何模仿UITableView的UITableViewStylePlain部分标题样式

我的应用程序使用UITableView部分标题标题中的缩写,VoiceOver很难发音。 由于我需要通过VoiceOver使这些标题可以发音,我需要给节标题标题一个accessibilityLabel

似乎这样做的唯一方法是绘制自定义节标题单元格。 我想模仿标准Apple UIKit为这些自定义部分标题提供的样式,但我不确定如何模仿Apple的这个元素的详细外观。

模仿UITableViewStylePlain部分标题样式的最佳方法是什么?

更新:我很清楚如何创建自定义标题单元格。 我正在寻找的是一种技术,可以模仿Apple为普通UITableView部分标题单元格提供的标题单元格样式。

如果有人仍然感兴趣,我看起来非常接近以下代码(使用上面评论中的Mark Adams图片,但我稍微调整了它们,因为我的应用程序也有横向模式):

 - (UIView *)tableView:(UITableView *)tbl viewForHeaderInSection:(NSInteger)section { UIView* sectionHead = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tbl.bounds.size.width, 18)]; sectionHead.backgroundColor = [UIColor colorWithWhite:0 alpha:0]; sectionHead.userInteractionEnabled = YES; sectionHead.tag = section; UIImageView *headerImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"PlainTableViewSectionHeader.png"]]; headerImage.contentMode = UIViewContentModeScaleAspectFit; [sectionHead addSubview:headerImage]; [headerImage release]; UILabel *sectionText = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, tbl.bounds.size.width - 10, 18)]; sectionText.text = text; sectionText.backgroundColor = [UIColor clearColor]; sectionText.textColor = [UIColor whiteColor]; sectionText.shadowColor = [UIColor darkGrayColor]; sectionText.shadowOffset = CGSizeMake(0,1); sectionText.font = [UIFont boldSystemFontOfSize:18]; [sectionHead addSubview:sectionText]; [sectionText release]; return [sectionHead autorelease]; } 

这是一个UILabel子类的实现,它以编程方式模拟背景:

UITableViewStandardHeaderLabel.h

 #import  @interface UITableViewStandardHeaderLabel : UILabel @property (nonatomic) CGFloat topInset; @property (nonatomic) CGFloat leftInset; @property (nonatomic) CGFloat bottomInset; @property (nonatomic) CGFloat rightInset; @end 

UITableViewStandardHeaderLabel.m:

 /*! * @class UITableViewStandardHeaderLabel * @brief Reimplementation of the UILabel used for a standard UITableView's group headers for customization purposes */ @implementation UITableViewStandardHeaderLabel @synthesize topInset, leftInset, bottomInset, rightInset; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; } return self; } - (void)drawTextInRect:(CGRect)rect { UIEdgeInsets insets = {self.topInset, self.leftInset, self.bottomInset, self.rightInset}; return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)]; } - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGGradientRef backgroundGradient; CGColorSpaceRef rgbColorspace; size_t num_locations = 2; CGFloat locations[2] = { 0.0f, 1.0f }; CGFloat components[8] = { 144.0f/255.0f, 159.0f/255.0f, 171.0f/255.0f, 1.0f, /* start */ 183.0f/255.0f, 192.0f/255.0f, 200.0f/255.0f, 1.0f /* end */ }; rgbColorspace = CGColorSpaceCreateDeviceRGB(); backgroundGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations); CGRect currentBounds = self.bounds; CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMinY(currentBounds)); CGPoint bottomCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMaxY(currentBounds)); CGContextDrawLinearGradient(context, backgroundGradient, topCenter, bottomCenter, 0); UIColor *topBorderLineColor = [UIColor colorWithRed:113.0f/255.0f green:125.0f/255.0f blue:133.0f/255.0f alpha:1.0]; UIColor *secondLineColor = [UIColor colorWithRed:165.0f/255.0f green:177.0f/255.0f blue:187.0f/255.0f alpha:1.0]; UIColor *bottomBorderLineColor = [UIColor colorWithRed:151.0f/255.0f green:157.0f/255.0f blue:164.0f/255.0f alpha:1.0]; [topBorderLineColor setFill]; CGContextFillRect(context, CGRectMake(0, 0, CGRectGetMaxX(currentBounds), 1)); [bottomBorderLineColor setFill]; CGContextFillRect(context, CGRectMake(0, CGRectGetMaxY(currentBounds)-1, CGRectGetMaxX(currentBounds), 1)); [secondLineColor setFill]; CGContextFillRect(context, CGRectMake(0, 1, CGRectGetMaxX(currentBounds), 1)); [super drawRect:rect]; } @end 

我发现其他答案要么不起作用,要么不模仿标准外观。 这是我的,适用于iOS 5和6。

请注意,如果您使用的是iOS 6,则应使用dequeueReusableHeaderFooterViewWithIdentifier ,这样可以使事情变得更加简单和清晰。

 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if ([tableView respondsToSelector:@selector(dequeueReusableHeaderFooterViewWithIdentifier:)]) { static NSString *headerReuseIdentifier = @"TableViewSectionHeaderViewIdentifier"; UITableViewHeaderFooterView *sectionHeaderView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerReuseIdentifier]; if(sectionHeaderView == nil){ sectionHeaderView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:headerReuseIdentifier]; } //customise the label here: //[sectionHeaderView.textLabel setTextColor:[UIColor whiteColor]]; return sectionHeaderView; } else { UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44.0)]; UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 10, 290, 0)]; headerLabel.backgroundColor = [UIColor clearColor]; headerLabel.text = [self tableView:tableView titleForHeaderInSection:section]; headerLabel.font = [UIFont boldSystemFontOfSize:17]; headerLabel.textAlignment = NSTextAlignmentLeft; headerLabel.shadowColor = [UIColor clearColor]; headerLabel.numberOfLines = 0; [headerLabel sizeToFit]; [headerView setFrame:CGRectMake(headerView.frame.origin.x, headerView.frame.origin.y, headerView.frame.size.width, headerLabel.bounds.size.height)]; //some customisation: headerLabel.textColor = [UIColor whiteColor]; [headerView addSubview: headerLabel]; return headerView; } } 

正如文档所说,如果实现viewForHeaderInSection ,则还必须实现heightForHeaderInSection 。 像这样实现它以确保它为任意数量的行获得正确的大小:

 -(float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return UITableViewAutomaticDimension; } 

我将创建一个自定义的UIView类,并为其标题文本添加一个UILabel。 对于背景,使用UIImageView并加载适当的图像作为节标题的背景。 使用适用于UIView的addSubView:方法为此UIImageView分配。

在UITableViewController中,您可以设置tableView.sectionHeaderHeight来自定义所有节标题的高度。 使用UITableViewDelegate方法:

 tableView:viewForHeaderInSection: 

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITableViewDelegate

您应该返回自定义UIView的实例,并将文本标签作为该部分的标题。

您应该为UILabel添加阴影并调整所有颜色以适合默认样式。 由于节标题也略微透明,您可以设置UIView alpha

 self.alpha = 0.9f;