ios UITableview 中各部分之间的空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30364067/
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-31 06:02:11  来源:igfitidea点击:

Space Between Sections in UITableview

iosuitableviewswiftlayout

提问by Joel

I need to reduce the space between two sections ofUITableView. I looked at this questionbut the solution doesn't allow for my custom header view because it combines the space of the footer and header.

我需要减少两个部分之间的空间UITableView。我查看了这个问题,但该解决方案不允许我的自定义页眉视图,因为它结合了页脚和页眉的空间。

Here is a picture of the UITableView. The black color is the UITableViewbackground color.

这是一张图片UITableView。黑色是UITableView背景色。

tableview screenshot

桌面截图

回答by Icaro

Did you try override this function:

您是否尝试覆盖此功能:

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

回答by Sudhin Davis

I think you can solve this by adjusting the footer view height to its min: in Storyboard or XIB.enter image description here

我认为您可以通过将页脚视图高度调整为最小值来解决这个问题:在 Storyboard 或 XIB 中。在此处输入图片说明

I don't know what you have written in your code for footer height. Sorry if I am wrong.

我不知道您在代码中为页脚高度编写了什么。对不起,如果我错了。

Possible duplicate of Hide footer view in UITableView

UITableView隐藏页脚视图的可能重复

回答by Ali Haris

For Swift 3

对于 Swift 3

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

回答by Aks

You need to use method heightForHeaderInSection for defining space between header & cell text. You can also change it depending on different sections for eg. at some sections you may need to show more distance & under some, you don't want to show gap. For such case you can use CGFLOAT_MIN which is 0.000001f. Giving you an example, how you can use different section with different header heights

您需要使用方法 heightForHeaderInSection 来定义标题和单元格文本之间的空间。您还可以根据不同的部分进行更改,例如。在某些部分,您可能需要显示更多距离,而在某些部分,您不想显示间隙。对于这种情况,您可以使用 CGFLOAT_MIN,即 0.000001f。举个例子,如何使用不同标题高度的不同部分

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0 || section == 2)
    {
        return 55.0;
    }
    else
    {
        return CGFLOAT_MIN;
    }
}

Hope it will help you.

希望它会帮助你。

回答by Nicholas Allio

Along with the answer posted by IcaroI would like to add that you also need to implement the tableView:viewForFooterInSection:method returning nilfor the section you want to remove the empty space below It will then become:

随着Icaro 发布答案,我想补充一点,您还需要实现为要删除下方空白区域的部分tableView:viewForFooterInSection:返回的方法nil,然后它将变为:

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

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

回答by Ladd.c

For Swift 4+ you need to implement these two methods

对于 Swift 4+ 你需要实现这两个方法

extension MyViewController : UITableViewDelegate {

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

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

}

回答by Mithra Singam

TableView Delegate methods doesn't effect with float value is 0.0f. Try giving a value greater than that.

TableView 委托方法不影响浮点值为 0.0f。尝试给出一个更大的值。

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

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

回答by polarware

This also may help :

这也可能有帮助:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.sectionHeaderHeight = UITableViewAutomaticDimension
}

回答by MichaelMao

You can do it by implement the delegate heightForHeaderInSection& heightForFooterInSection.

您可以通过实现委托heightForHeaderInSection&来做到这一点heightForFooterInSection

The return vaule should not be 0, even if the SectionHeader or the height of SectionFooter is 0, it need a very small value, try CGFLOAT_MIN.

返回值不应为0,即使SectionHeader或SectionFooter的高度为0,也需要很小的值,试试吧CGFLOAT_MIN

for my example:

对于我的例子:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == [self.dataArray indexOfObject:self.bannerList]) {
    return 46;
}
return CGFLOAT_MIN;

}

}

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

回答by Fernando Cardenas

For Swift 5+:

对于 Swift 5+:

There is some space for the headers and footers by default. That's why I was having the problem of setting an exact separation for the sections.

默认情况下,页眉和页脚有一些空间。这就是为什么我遇到了为各个部分设置精确分隔的问题。

My solution to have a separation between 2 sections is the following:

我将两个部分分开的解决方案如下:

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

        if section == 0 {
             return 24
        } else if section == 1 {
             return 32
        }
    }

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

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

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

As you see for viewForFooterInSection and viewForHeaderInSection I needed to return nil.

正如您在 viewForFooterInSection 和 viewForHeaderInSection 中看到的,我需要返回 nil。

In case you only want to change the footerHeight, just return CGFloat.leastNormalMagnitude for heightForHeaderInSection, and return the heights for each section in heightForFooterInSection.

如果您只想更改footerHeight,只需为heightForHeaderInSection 返回CGFloat.leastNormalMagnitude,并在heightForFooterInSection 中返回每个部分的高度。