如何更改分组UITableView标头的高度?

cir*_*ego 83 uitableview uikit tableview ios

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

现在我有这个代码:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0){
        return 0;
    }
    return 10;
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Mar*_*nek 211

返回CGFLOAT_MIN0而不是0表示所需的截面高度.

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

斯威夫特3:

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

迅速:

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

OBJ-C:

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

  • 在Swift中,'CGFLOAT_MIN'不可用:改为使用CGFloat.min. (8认同)
  • 在Swift 3中,它是`CGFloat.leastNormalMagnitude` (5认同)
  • CGFloat.min导致崩溃,因为CGFloat.min返回负值,如-0.0000000000001 (4认同)
  • 也许这是迂腐,但CGFloat.min不是一个非常小的数字,它是一个非常大的负数.如果你想要一个非常小的数字,你会使用epsilon. (2认同)

Jak*_*lář 24

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

Objective-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;
}
Run Code Online (Sandbox Code Playgroud)

迅速

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
}
Run Code Online (Sandbox Code Playgroud)


cir*_*ego 19

似乎我无法设置高度为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];
}
Run Code Online (Sandbox Code Playgroud)


FBe*_*nte 15

这适用于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)))
Run Code Online (Sandbox Code Playgroud)

  • 救星,感谢您花时间在这里提及它:) (2认同)

小智 13

你可以试试这个:

在里面 loadView

_tableView.sectionHeaderHeight = 0;
Run Code Online (Sandbox Code Playgroud)

然后

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

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

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

同样如果你没有删除sectionfooter.

_tableView.sectionFooterHeight = 0;
Run Code Online (Sandbox Code Playgroud)

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

嗯,这适用于iOS7中tableview的问题.