减少UITableView各部分之间的空间

flo*_*hei 139 iphone uitableview ios

有没有办法减少UITableView的两个部分之间的空间?每个部分之间大约有15个像素.我确实已经尝试返回0 -tableView:heightForFooterInSection:,-tableView:heightForHeaderInSection:但这并没有改变任何东西.

有什么建议?

Tom*_*men 261

这有点棘手,但请尝试以下代码:

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

    return 1.0;
}

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

- (UIView*)tableView:(UITableView*)tableView 
           viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView*)tableView:(UITableView*)tableView 
           viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}
Run Code Online (Sandbox Code Playgroud)

相应地更改值.要删除空格,我认为不会接受0.0.最小的理智值似乎是1.0.

  • 您甚至可以返回0.00001f作为高度,您将获得0像素/点的高度. (45认同)
  • 为什么不使用CGFLOAT_MIN?它是为这种情况而制作的:) (26认同)
  • 如果使用swift,则可以返回`CGFloat.leastNonzeroMagnitude` (10认同)
  • 你应该返回5.0f而不是5.0,因为双浮动冲突 (2认同)

Mar*_*olz 141

对于想要将距离缩小到0的所有人,您必须使用:

tableView.sectionHeaderHeight = 0.0;
tableView.sectionFooterHeight = 0.0;
Run Code Online (Sandbox Code Playgroud)

因为UITableViewDelegate的服务仅从大于零的浮点数开始生效.

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


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

-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
}

-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
    return [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
}
Run Code Online (Sandbox Code Playgroud)

(使用iOS 4.1和XCode 4.0.2)

  • 返回0.0001; 正在为我工​​作.我使用的是xcode 4.6和iOS 6.1 (7认同)

小智 33

您可以在"大小"选项卡下的Interface Builder中实际设置页脚/页眉/单元格高度.默认情况下,页眉/页脚设置为10.0.

  • 我知道这已经过时了,但我想评论一下,从XCode 5和iOS 7开始,似乎这就是在代码中完成的.我在界面构建器中将它设置为0,它仍设置为1,直到我将其在代码中设置为0. (2认同)

Jir*_*une 27

您必须减少节页眉/页脚高度.然后部分之间的空间将减少.

试试这段代码

这个对我有用 :-)

tableView.sectionHeaderHeight = 2.0;
tableView.sectionFooterHeight = 2.0;
Run Code Online (Sandbox Code Playgroud)


ios*_*tor 21

您还可以从故事板中减小部分页脚和页眉的高度.在tableview - > size inspector中.转到章节高度.

故事板中UITableView的大小检查器.

默认情况下,对于普通样式表,它设置为22,对于分组样式表,它设置为10.您可以通过分别增加/减少页眉和页脚的值来配置值.


Lir*_*dav 6

对我来说,问题是因为我使用的是分组的表格视图样式(UITableViewStyleGrouped)。当我将其更改为简单样式(UITableViewStylePlain)时,我的tableView:heightForHeaderInSection:方法是确定每个节标题的大小的唯一方法。


sta*_*tan 6

也许使用这个,0 不会按预期工作

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

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return .leastNormalMagnitude
}
Run Code Online (Sandbox Code Playgroud)