【问题标题】:How to change height of grouped UITableView header?如何更改分组 UITableView 标题的高度?
【发布时间】:2013-07-16 00:12:26
【问题描述】:

我知道如何在表格视图中更改节标题的高度。但我找不到任何解决方案来更改第一部分之前的默认间距。

现在我有这个代码:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0){
        return 0;
    }
    return 10;
}

【问题讨论】:

标签: ios uikit tableview uitableview


【解决方案1】:

这两行就够了。

tableView.sectionHeaderHeight = 0
tableView.sectionFooterHeight = 0
    

对我来说,它适用于 Xcode 12.2、iOS 14.2

【讨论】:

    【解决方案2】:

    在 Swift 4 中

    删除分组表视图中多余的顶部填充。

    这里的高度被指定为 1 作为节标题的最小高度,因为您不能指定 0,因为如果指定高度为零,tableview 将采用默认的上边距。

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 1
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }
    

    【讨论】:

      【解决方案3】:

      添加后应删除代码self.tableView.tableHeaderView = [UIView new];

      - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
          return CGFLOAT_MIN;
      }
      

      【讨论】:

      • 这是footer 高度在我的情况下导致问题。感谢您的帮助。
      【解决方案4】:

      这对我来说适用于 Swift 4。修改您的 UITableView 例如在viewDidLoad:

      // Remove space between sections.
      tableView.sectionHeaderHeight = 0
      tableView.sectionFooterHeight = 0
      
      // Remove space at top and bottom of tableView.
      tableView.tableHeaderView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 0, height: CGFloat.leastNormalMagnitude)))
      tableView.tableFooterView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 0, height: CGFloat.leastNormalMagnitude)))
      

      【讨论】:

      • 救命稻草,感谢您抽出宝贵时间在此提及 :)
      【解决方案5】:

      如果您使用tableView 样式分组tableView 会自动设置顶部和底部插入。要避免它们并避免内部插入设置,请对页眉和页脚使用委托方法。 永远不要返回 0.0,而是 CGFLOAT_MIN

      目标-C

      - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
          // Removes extra padding in Grouped style
          return CGFLOAT_MIN;
      }
      
      - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
          // Removes extra padding in Grouped style
          return CGFLOAT_MIN;
      }
      

      斯威夫特

      func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
          // Removes extra padding in Grouped style
          return CGFloat.leastNormalMagnitude
      }
      
      func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
          // Removes extra padding in Grouped style
          return CGFloat.leastNormalMagnitude
      }
      

      【讨论】:

      • 我还必须为viewForHeaderInSection 返回 nil 以使标题完全消失。
      【解决方案6】:

      返回 CGFLOAT_MIN 而不是 0 以获得所需的截面高度。

      返回 0 会导致 UITableView 使用默认值。这是 无证行为。如果你返回一个非常小的数字,你 有效地得到一个高度为零的标题。

      斯威夫特 3:

       func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
              if section == 0 {
                  return CGFloat.leastNormalMagnitude
              }
              return tableView.sectionHeaderHeight
          }
      

      斯威夫特:

      func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
          if section == 0 {
              return CGFloat.min
          }
          return tableView.sectionHeaderHeight
      }
      

      对象-C:

          - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
      {
          if (section == 0)
              return CGFLOAT_MIN;
          return tableView.sectionHeaderHeight;
      }
      

      【讨论】:

      • 在 Swift 中,'CGFLOAT_MIN' 不可用:请改用 CGFloat.min。
      • CGFloat.min 导致崩溃,因为 CGFloat.min 返回负值,如 -0.0000000000001
      • 也许这是迂腐,但 CGFloat.min 不是一个很小的数字,它是一个很大的负数。如果你想要一个非常小的数字,你会使用 epsilon。
      • 在 Swift 3 中是CGFloat.leastNormalMagnitude
      • 建议:不要在estimatedHeightForHeaderInSection上使用这个值,应用会崩溃。
      【解决方案7】:

      在 Swift 2.0 中

      func tableView(tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
      
              return yourHeight
          }
      

      【讨论】:

        【解决方案8】:

        你可以试试这个:

        loadView

        _tableView.sectionHeaderHeight = 0;
        

        然后

        -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
            return 0;
        }
        

        只要标题中没有任何对象,就应该将其删除...

        如果你想要一些大小的sectionheader,那么只改变返回值。

        如果您不删除 sectionfooter,则相同。

        _tableView.sectionFooterHeight = 0;
        

        -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
            return 0;
        }
        

        嗯,这适用于我在 iOS7 中的 tableview 问题。

        【讨论】:

          【解决方案9】:

          您可以使用viewForHeaderInSection 并返回任意高度的视图。

          - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
          {
          
              int height = 30 //you can change the height 
              if(section==0)
              {
                 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, height)];
          
                 return view;
              }
          }
          

          【讨论】:

          • 我问的不是节标题,而是表标题。
          • 那么就可以直接把一个uiview传给表头视图了。
          【解决方案10】:

          viewForHeaderInSection 示例:

          - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
          UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 118)];
          view.backgroundColor = COLOR_DEFAULT;
          
          NSString* key = [self.tableKeys objectAtIndex:section];
          NSArray *result = (NSArray*)[self.filteredTableData objectForKey:key];
          SZTicketsResult *ticketResult = [result objectAtIndex:0];
          
          UIView *smallColoredView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 320, 3)];
          smallColoredView.backgroundColor = COLOR_DEFAULT_KOSTKY;
          [view addSubview:smallColoredView];
          
          UIView *topBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 8, 320, 40)];
          topBackgroundView.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:248.0/255.0 blue:174.0/255.0 alpha:1];
          [view addSubview:topBackgroundView];
          
          UILabel *totalWinnings = [[UILabel alloc] initWithFrame:CGRectMake(10, 8, 300, 40)];
          totalWinnings.text = ticketResult.message;
          totalWinnings.minimumFontSize = 10.0f;
          totalWinnings.numberOfLines = 0;
          totalWinnings.backgroundColor = [UIColor clearColor];
          totalWinnings.font = [UIFont boldSystemFontOfSize:15.0f];
          [view addSubview:totalWinnings];
          
          UIView *bottomBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 55, 320, 58)];
          bottomBackgroundView.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:248.0/255.0 blue:174.0/255.0 alpha:1];
          [view addSubview:bottomBackgroundView];
          
          UILabel *numberOfDraw = [[UILabel alloc] initWithFrame:CGRectMake(10, 55, 290, 58)];
          numberOfDraw.text = [NSString stringWithFormat:@"sometext %@",[ticketResult.title lowercaseString]];;
          numberOfDraw.minimumFontSize = 10.0f;
          numberOfDraw.numberOfLines = 0;
          numberOfDraw.backgroundColor = [UIColor clearColor];
          numberOfDraw.font = [UIFont boldSystemFontOfSize:15.0f];
          [view addSubview:numberOfDraw];
          
          return view;
          

          【讨论】:

            【解决方案11】:

            看来我无法设置高度为 0 的表头视图。我最终执行了以下操作:

            - (void)viewWillAppear:(BOOL)animated{
                CGRect frame = self.tableView.tableHeaderView.frame;
                frame.size.height = 1;
                UIView *headerView = [[UIView alloc] initWithFrame:frame];
                [self.tableView setTableHeaderView:headerView];
            }
            

            【讨论】:

            • 这里最好设置高度:- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 1.0f; }
            猜你喜欢
            • 2023-03-10
            • 1970-01-01
            • 2010-11-17
            • 2013-08-05
            • 1970-01-01
            • 2016-07-05
            • 2012-07-26
            • 2023-04-07
            • 1970-01-01
            相关资源
            最近更新 更多