【问题标题】:How to center the section headers of a UITableView如何使 UITableView 的部分标题居中
【发布时间】:2011-02-02 14:10:10
【问题描述】:

我想在 tableview 上将标题居中,但是我有 2 个问题。首先,我没有正确的高度,其次 UILabel 看起来不像默认标题 - 字体/字体大小/颜色等......有没有更好的方法来居中,和/或有没有办法制作它看起来像默认标题。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger) section
{
    //section text as a label
    UILabel *lbl = [[UILabel alloc] init];
    lbl.textAlignment = UITextAlignmentCenter;  

    lbl.text = @"Header";
    [lbl setBackgroundColor:[UIColor clearColor]];  

    return lbl;
}

【问题讨论】:

    标签: ios uitableview cocoa-touch


    【解决方案1】:

    这应该可以解决您的问题。在 xcode 6 / ios8 中测试

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        [[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextAlignment:NSTextAlignmentCenter];
        return [sectionTitles objectAtIndex:section];
    }
    

    【讨论】:

    • 这很好用。我以前没有使用或看到外观作为在视图层次结构中查找某些内容的方法。忍术!
    • 有效! “appearanceWhenContainedIn”可以从方法中取出并放在viewDidLoad中
    • 在应用程序委托的 - application:didFinishLaunchingWithOptions: 中包含 appearanceWhenContainedIn,它将应用于整个应用程序。
    【解决方案2】:

    你还必须实现 tableView:heightForHeaderInSection 来调整你的标题高度:

    在 UITableViewDelegate 协议参考文档中,您可以找到:

    tableView:viewForHeaderInSection:

    讨论

    例如,返回的对象可以 是一个 UILabel 或 UIImageView 对象。 表格视图自动调整 节标题的高度为 容纳返回的视图对象。 此方法仅在以下情况下才能正常工作 表视图:heightForHeaderInSection:是 也实施了。

    要使 Header 标签居中,您必须在将其对齐设置为居中之前指定标签框架。 要获取标准字体,请使用 SystemFontOfSize:

    还要注意您正在造成内存泄漏您应该返回一个自动释放的视图

    试试这样的:

    UILabel *lbl = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)] autorelease];
    lbl.textAlignment = UITextAlignmentCenter;
    lbl.font = [UIFont systemFontOfSize:12];
    

    希望这会有所帮助, 文森特

    【讨论】:

    • 谢谢,但我仍然需要找到实际默认标题的字体大小、标签高度等,以便将其设置为如上...。我不想从除中心对齐外的默认标题。左对齐对于我正在做的事情来说看起来很糟糕。
    • 我接受了这个答案,因为它很有帮助,我决定不使用手机的默认标题字体,而是使用我自己的。
    • 在CGRectMake中必须是self.view.frame.size.width(0, 0, self.view.frame.width, 30)
    【解决方案3】:

    Volker 在 Swift 中的回答:

    如果您的目标是 iOS6 及更高版本,如果您只想将标题标题居中,则不必提供自己的标题视图。

    只需执行

    - tableView:willDisplayHeaderView:forSection:
    

    并设置标签的 textAligment 属性:

    func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        if let headerView = view as? UITableViewHeaderFooterView {
            headerView.textLabel?.textAlignment = .Center
        }
    }
    

    【讨论】:

      【解决方案4】:

      如果您的目标是 iOS6 及更高版本,如果您只想使标题标题居中,则不必提供自己的标题视图。

      只需执行

      - tableView:willDisplayHeaderView:forSection:

      并设置标签的textAligment属性:

      - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
      {
          if([view isKindOfClass:[UITableViewHeaderFooterView class]]){
      
              UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *) view;
              tableViewHeaderFooterView.textLabel.textAlignment = NSTextAlignmentCenter;
          }
      }
      

      【讨论】:

      • 这应该是公认的答案。使用 [UILabel appearanceWhenContainedIn:UITableViewHeaderFooterView.class...] 技巧将更改 all 您在 all 应用程序中的 tableviews 中的标题
      【解决方案5】:

      要调用这个方法,你应该首先实现

      titleForHeaderInSection
      

      然后tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)方法将被调用

      Swift 解决方案:

      func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
          if let headerView = view as? UITableViewHeaderFooterView {
      
      
              headerView.textLabel?.textAlignment = Localize().isRTL ? .Right : .Left
              headerView.textLabel?.text = partsDataSource[section]
              headerView.textLabel?.textColor = UIColor ( red: 0.0902, green: 0.2745, blue: 0.2745, alpha: 1.0 )
              headerView.textLabel?.font = UIFont(name: UIDecorator.sharedInstance.PRIMARY_FONT, size: 14.0)
              headerView.contentView.backgroundColor = UIDecorator.sharedInstance.currentTheme.lightShadeColor
          }
      }
      
      func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
          return " "
      }
      

      【讨论】:

        【解决方案6】:

        这是我的版本,您需要截取正常标题背景的屏幕截图,并将其 1px 宽的切片保存为“my_head_bg.png”并将其添加到项目中。这样,它看起来就完全正常了:

        -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
            UILabel *lbl = [[[UILabel alloc] init] autorelease];
            lbl.textAlignment = UITextAlignmentCenter;
            lbl.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
            lbl.text = @"My Centered Header";
            lbl.textColor = [UIColor whiteColor];
            lbl.shadowColor = [UIColor grayColor];
            lbl.shadowOffset = CGSizeMake(0,1);
            lbl.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"my_head_bg"]];
            lbl.alpha = 0.9;
            return lbl;
        }
        

        【讨论】:

          【解决方案7】:

          试试这个

              UILabel *tableHeader = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
          [tableHeader setText:@"This is header"];
          [tableHeader setTextAlignment:UITextAlignmentCenter];
          
          UITableView *newTable = [[UITableView alloc] initWithFrame:CGRectMake(20, 0, 280, 200) style:UITableViewStylePlain];
          [newTable setBackgroundColor:[UIColor clearColor]];
          [newTable setTableHeaderView:tableHeader];
          self.view = newTable;
          

          【讨论】:

            【解决方案8】:

            在 Xamarin 中:

                    // Center Header
                    public override void WillDisplayHeaderView(UITableView tableView, UIView headerView, nint section)
                    {
                        if (headerView.GetType() == typeof(UITableViewHeaderFooterView))
                        {
                            UITableViewHeaderFooterView tableViewHeaderFooterView = (UITableViewHeaderFooterView)headerView;
                            tableViewHeaderFooterView.TextLabel.TextAlignment = UITextAlignment.Center;
                        }
                    }
            

            【讨论】:

              【解决方案9】:

              要改变高度,实现这个方法:

              - (UIView *)tableView:(UITableView *)tableView 
              viewForHeaderInSection:(NSInteger)section { return 15; }

              我不太确定默认标题的事情 - 这应该比它更容易。也许有人有一个漂亮的背景图像,你可以使用白色字体。但是对于居中的事情,试试这个:

              UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,320,15)];
              label.textAlignment = UITextAlignmentCenter
              
              label.text = @"Text Should Be Centered";
              

              【讨论】:

              • 我认为您需要在提交之前检查您的代码! -viewForHeaderInSection 函数返回 UIView 不能返回 15。
              • @HamedRajabi 我想我可能打算将 15 返回到所谓的高度方法:P
              猜你喜欢
              • 1970-01-01
              • 2023-03-16
              • 1970-01-01
              • 1970-01-01
              • 2013-10-05
              • 1970-01-01
              • 1970-01-01
              • 2017-07-06
              • 1970-01-01
              相关资源
              最近更新 更多