ios 减少 UITableView 部分之间的空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2817308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:14:02  来源:igfitidea点击:

Reducing the space between sections of the UITableView

iphoneiosuitableview

提问by flohei

Is there a way to reduce the space between two sections of a UITableView? There are about 15 pixels between every single section I have. I did already try to return 0 for -tableView:heightForFooterInSection:and -tableView:heightForHeaderInSection:but that doesn't change anything.

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

Any suggestions?

有什么建议?

回答by Tomen

It was a bit tricky, but try this code:

这有点棘手,但试试这个代码:

- (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];
}

Change the values accordingly. To remove the space, I think 0.0 will not be accepted. The smallest sane value seems to be 1.0.

相应地更改值。去掉空格,我认为0.0不会被接受。最小的理智值似乎是 1.0。

回答by Martin Stolz

For all who want to shrink the distance to 0 you have to use:

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

tableView.sectionHeaderHeight = 0.0;
tableView.sectionFooterHeight = 0.0;

Because the serving of the UITableViewDelegate does only make an effect starting from floats greater than zero.

因为 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];
}

(using iOS 4.1 with XCode 4.0.2)

(使用 iOS 4.1 和 XCode 4.0.2)

回答by Chris

You can actually set the footer/header/cell heights in Interface Builder under the size tab. By default the header/footer are set at 10.0.

您实际上可以在尺寸选项卡下的界面生成器中设置页脚/页眉/单元格高度。默认情况下,页眉/页脚设置为 10.0。

回答by Jirune

You have to reduce the section header/footer height. Then the space between sections will be reduce.

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

try this code

试试这个代码

It works for me :-)

这个对我有用 :-)

tableView.sectionHeaderHeight = 2.0;
tableView.sectionFooterHeight = 2.0;

回答by iosCurator

You can also reduce the height of section footer and header from the storyboard. In the tableview -> size inspector. Go to Section Height.

您还可以从情节提要中降低节页脚和页眉的高度。在 tableview -> 尺寸检查器中。转到截面高度。

Size inspector for UITableView in storyboard.

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

By default it is set to 22 for Plain style table and 10 for grouped style table. You can configure values by increasing / decreasing the values for header and footer separately.

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

回答by Liron Yahdav

For me the issue was because I was using a grouped table view style (UITableViewStyleGrouped). When I changed it to a plain style (UITableViewStylePlain) my tableView:heightForHeaderInSection:method was the only thing determining the size of each section header.

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

回答by stan

Use this perhaps, 0 will not work as expected

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

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

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

回答by Chisx

UPDATE FOR iOS7:Due to ARC autorelease update, here's @Martin Stolz code edited.

iOS7更新由于 ARC 自动发布更新,这里是 @Martin Stolz 代码编辑。

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

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

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

-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}

(Using iOS7, Xcode v5.0)

(使用iOS7,Xcode v5.0)

回答by Murlakatam

To change header/footer space the following methods have tobe implemented:

要更改页眉/页脚空间必须实现以下方法:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?

AND

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat

(use corresponding methods to change footer height)

(使用相应的方法改变页脚高度)

The following code completely removes spaces around sections:

以下代码完全删除部分周围的空格:

public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return nil
}

public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    return nil
}

public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return .leastNonzeroMagnitude
}

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

回答by Bharath

For me just this issue got resolved just by using the following code,

对我来说,只是通过使用以下代码解决了这个问题,

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return section == 0 ? 1 : 20 // 20 is my other sections height
}

Returning 1for the first section has solved the issue.

为第一部分返回1已经解决了这个问题。