【问题标题】:Hide TableViewHeaderView for a specific TableView section隐藏特定 TableView 部分的 TableViewHeaderView
【发布时间】:2013-07-28 20:59:13
【问题描述】:

所以我有这个包含几个部分的 Tableview,确切地说是 (3)。我希望第 2 节和第 3 节有标题,而不是第一节..

这是我所做的:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSString *sectionName;

    UIView *tempView;
    tempView = [[UIView alloc]initWithFrame:CGRectMake(0,0,300,20)];
    tempView.backgroundColor=[UIColor grayColor];

    UILabel *tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,0,300,20)];
    tempLabel.backgroundColor = [UIColor clearColor];
    tempLabel.shadowColor = [UIColor blackColor];
    tempLabel.shadowOffset = CGSizeMake(0,2);
    tempLabel.textColor = [UIColor whiteColor]; //here you can change the text color of header.
    tempLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0f];


    switch (section)
    {
            break;
        case 1:
        {
            sectionName = NSLocalizedString(@"Information", @"Information");
            tempLabel.text = sectionName;
            [tempView addSubview:tempLabel];
        }
            break;

        case 2:
        {
            sectionName = NSLocalizedString(@"Tools", @"Tools");
            tempLabel.text = sectionName;
            [tempView addSubview:tempLabel];
        }
            break;

    }
    return tempView;
}

我对需要做什么感到困惑......这是正在发生的事情的图片:

【问题讨论】:

    标签: iphone ios objective-c uitableview tableview


    【解决方案1】:

    在 section == 0 的情况下,您仍在将 tempView 设置为 UIView 的新实例并返回该值,您只是没有设置标签的标题。此外,正如您所了解的,您应该为第 0 部分的 tableView:heightForHeaderInSection: 返回 0。

    【讨论】:

      【解决方案2】:

      所以当我写这个问题时,我得出了一个结论......但也许它不是更有效?

      - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
      {
          CGFloat headerheight = 20.0;
      
          switch (section)
          {
              case 0: { headerheight = 0.0; } break;
              case 1: { headerheight = 20.0; } break;
              case 2: { headerheight = 20.0; } break;
          }
          return headerheight;
      }
      

      如果有人对我如何不需要实现这个 tableview 委托方法有任何建议,请说出来。我觉得我只是不需要返回指定部分的视图而不是说部分标题为 0。但我目前不完全确定,问题已解决但可能没有正确解决?

      这是解决方案结果的图片。

      【讨论】:

      • 是的,您还应该为 tableView:heightForHeaderInSection: 返回 0,但最好将 viewForHeaderInSection 修改为在该部分 == 0 的情况下返回 nil。在该方法的顶部。
      • 您认为您可以为我实现 section == 0 案例吗?我像这样实现了 viewForHeaderInSection 方法, switch (section) { case 0: { tableView.tableHeaderView = nil; } 休息;它没有工作:(
      • 只需将其添加为该方法的第一行:if (section == 0) return nil;。如果你这样做,你就不需要case 0(而且,你不会有创建一个你最终会丢弃的标题视图的开销)。
      猜你喜欢
      • 2013-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-14
      • 1970-01-01
      • 2016-01-04
      相关资源
      最近更新 更多