繁体   English   中英

如何更改 GROUPED UITableView 中各部分之间的高度?

[英]How to change the height BETWEEN sections in GROUPED UITableView?

默认高度对我来说太大了。 如何更改两个部分之间的高度?

==================================================== ===========================

对不起,我的英语很差...我的意思是两个部分之间的差距太大了。 如何改变它?

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    CGRect frame = CGRectMake(0, 0, tableView.frame.size.width, 2.0f);
    UIView *view = [[[UIView alloc] initWithFrame:frame] autorelease];
    view.backgroundColor = [UIColor blueColor];
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    CGRect frame = CGRectMake(0, 0, tableView.frame.size.width, 2.0f);
    UIView *view = [[[UIView alloc] initWithFrame:frame] autorelease];
    view.backgroundColor = [UIColor redColor];
    return view;
}

我更改了每个部分的 header 视图和页脚视图。 结果如下:在此处输入图像描述

我认为可能有一个最小高度的差距,因为我将header和footer的高度设置为2px,但是两个部分之间的差距仍然很大。

您可以在每个部分之间使用页脚视图,并通过在 UITableView 的委托中实现以下方法来为它们提供所需的空间大小。

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

在第一个委托方法中,您可以返回一个简单的 UIView 实例,在第二个委托方法中,您可以返回此视图应具有的高度。 第二个委托方法应该允许您控制部分之间的差距。

在 iOS 15 上,您可以更改 header 顶部填充部分

if #available(iOS 15.0, *) {
    tableView.sectionHeaderTopPadding = 0
}

试试这个代码

self.tableView.rowHeight = 0; // in viewdidload
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; //    in viewdidload

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

-(CGFloat)tableView:(UITableView *)tableView    heightForHeaderInSection:(NSInteger)section{
    return <your header height>;
}

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

- (UIView *)tableView:(UITableView *)tableView  viewForHeaderInSection:(NSInteger)section{
    return <your header view>;
}

Swift 版本是

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

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

    return your footer view height
}

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

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return UIView.init(frame: CGRect.zero)
}

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

CGFLOAT_MIN 即 0.000001f

. 举个例子,如何使用不同标题高度的不同部分

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

希望它会帮助你。

对于那些想要增加距离的人。

基于设置页眉/页脚高度的解决方案可能有一个副作用:如果您有一些用于页眉或页脚的文本,它将在增加的页眉/页脚内垂直居中。

要解决此问题,您可以使用自定义页眉/页脚视图,但我建议在需要额外间隙的地方添加一个空白部分并为其设置页脚高度(将“”设置为页脚文本,以防为零或空字符串不会显示)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM