【问题标题】:How to mimic UITableView's UITableViewStylePlain section header style如何模仿 UITableView 的 UITableViewStylePlain 部分标题样式
【发布时间】:2011-06-07 13:45:12
【问题描述】:

我的应用程序在UITableView 部分标题标题中使用了 VoiceOver 难以发音的缩写。因为我需要让 VoiceOver 能够发音这些标题,所以我需要给部分标题标题一个 accessibilityLabel。

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

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

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

【问题讨论】:

    标签: ios objective-c uitableview cocoa-touch uikit


    【解决方案1】:

    如果有人仍然感兴趣,我已经用下面的代码让它看起来非常接近(使用上面评论中的 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];
    }
    

    【讨论】:

      【解决方案2】:

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

      UITableViewStandardHeaderLabel.h

      #import <UIKit/UIKit.h>
      
      @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
      

      【讨论】:

      • 正是我想要的。不敢相信这没有更多的选票!
      【解决方案3】:

      我发现其他答案要么不起作用,要么不模仿标准外观。这是我的,适用于 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;
      }
      

      【讨论】:

      • 对于遇到这个问题的任何人,如果 registerClass:forHeaderFooterViewReuseIdentifier: 被调用,例如在 viewDidLoad 中,然后 dequeueReusableHeaderFooterViewWithIdentifier: 将始终返回一个视图,并且可以省略以下对 nil 的测试。
      【解决方案4】:

      我将创建一个自定义 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;
      

      【讨论】:

      • 我确实知道如何实现我自己的标题单元格,这非常简单。但是,我想完全模仿 Apple 提供的普通 UITableView 的默认节标题样式的外观。我将更新我的问题以澄清这一点。
      • 如果你想完全模仿它我不确定,因为它们是 UIView 对象而不是它们自己的特殊类。
      • 我制作了一些自定义图像用于部分标题。视网膜准备好了。您应该能够使用 UILabel 完成文本样式。 cl.ly/4Joo
      猜你喜欢
      • 2013-12-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 2012-01-26
      相关资源
      最近更新 更多