iPhone UITableView:如何删除组样式表中各部分之间的间距?

问题描述 投票:0回答:11

我正在创建一个表格视图,其中有 10 个部分,所有部分都有标题视图,但没有单元格。所以,简而言之,我的表视图将仅显示 10 个标题视图;任何部分都不会有单元格。现在,当我这样做时,该部分的标题视图之间有一些空间。我想删除那个空格。那可能吗?您能为我提供一些提示或解决方法来实现这一目标吗?

数据来源方法如下:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return 10;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel * label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor grayColor];
    return label;
}

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

这是输出的屏幕截图: enter image description here

我这样做的原因有点复杂,我无法通过写作来解释。我想要做的就是在该部分的标题视图中没有间距。预先感谢您。

ios iphone objective-c uitableview tableview
11个回答
51
投票

试试这个..

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

还有表格分隔符为无。


23
投票

如果您在

0
中返回
tableView:heightForFooterInSection:
,则返回默认值(可能是 10.0)。这很棘手,但您可以使用
CGFLOAT_MIN
而不是
0
来删除页脚。

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

更新:

斯威夫特3

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

4
投票

您可以直接在界面中执行此操作,您必须将页脚高度设置为1,如下所示:

enter image description here


3
投票

尝试使用以下代码

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.25; // as for you need
}

您也可以通过以下委托方法来管理

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

很短你可以通过以上两种方法来管理。


3
投票

分组样式的tableview有默认的页脚,您还应该自定义页脚来覆盖它

尝试朴素风格。

typedef enum {
   UITableViewStylePlain,
   UITableViewStyleGrouped
} UITableViewStyle;

1
投票

将表视图样式从“分组”更改为“普通”。这解决了我的问题。


1
投票

所以我不能投票,但我可以发表回复

其实这样的正确答案

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

0
投票

您需要使用方法 heightForHeaderInSection 。您还可以根据不同的部分更改它,例如。在某些部分,您可能需要显示更多距离,而在某些部分,您不想显示间隙。对于这种情况,您可以使用 CGFLOAT_MIN,即 0.000001f。给您一个例子,如何使用具有不同标题高度的不同部分

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

希望对您有帮助。


0
投票

斯威夫特5

如果你使用带有.grouped样式的tableview,也许你应该像这样设置标题委托方法:

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

0
投票

斯威夫特5+

    if #available(iOS 15.0, *) {
        self.tableView.sectionHeaderTopPadding = 0
    }
    else {
        // it will fix from properties of table view
    }

enter image description here


-1
投票

尝试使用这个:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
   return 0;
}

正如评论中提到的,尝试使用这个:

 self.tableView.rowHeight = 0;
© www.soinside.com 2019 - 2024. All rights reserved.