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

18

我正在创建一个表视图,其中有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;
}

这是输出的截图: 在此输入图片描述

我有一个比较复杂的原因,为什么我要这样做,无法通过写作来解释。我想做的就是在部分标题视图中没有间距。提前谢谢。


2
你想要10个分区标题和没有单元格吗?那么用10个看起来像分区标题的单元格做一个表视图怎么样? - Michael Dautermann
1
我同意Michael的观点,你应该只使用单元格而不是表头。 - rahul
1
@maddy 我明白使用10个单元格可以解决问题,但正如我之前提到的,我使用标题视图的原因有点复杂,我无法解释清楚。我创建了一种可展开的单元格视图。再次强调,它不完全像可展开的视图,而是有很多其他限制条件。 - Bharat Jagtap
10个回答

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

同时将表格分隔线设为无。


3
是的,它有效了,我只是从heightForFooterInSection方法返回0.01f,然后它就起作用了。非常感谢。如果您可以编辑答案,使其变成0.01,我可以将其标记为正确答案。 - Bharat Jagtap
@RAJA- 非常感谢。这真的是一个聪明的答案,我刚刚没想到那个。 - Mohd Haider
1
为什么它不能只是0呢? - Carlos

23
如果您在tableView:heightForFooterInSection:中返回0,则会返回默认值(可能是10.0)。这有些棘手,但您可以使用CGFLOAT_MIN代替0来删除页脚。
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

更新:

Swift 3:

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

太棒了,它完美地运行了,而且我在标题中遇到了问题,但它也在那里工作了。 - Abhishek Thapliyal

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

简单来说,您可以通过上述两种方法来管理它。

没有页脚,因此使用该委托不会改变任何内容。 - rmaddy
我尝试了使用 heightForFooterInSection,并且在分组的tableView中它对我起作用。 - iPatel
你是否也实现了 titleForFooterInSectionviewForFooterInSection 中的任意一个?只有在实现了这两个方法中的任何一个时,heightForFooterInSection 才会起作用。 - rmaddy
有趣。我刚试了一下,实现 heightForFooterInSection 确实对组节之间的间距产生了一些影响,但你无法完全删除这个空间。 - rmaddy
@rmaddy - 你可以使用 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 并设置 return 0; 来完全删除两个部分之间的空格。 :) - iPatel
显示剩余2条评论

3
分组样式的表格视图有一个默认的页脚,您应该自定义页脚以覆盖它,或者尝试使用普通样式。
typedef enum {
   UITableViewStylePlain,
   UITableViewStyleGrouped
} UITableViewStyle;

1
所以我不能点赞,但我可以发表回复。
事实上,这种方式的正确答案。
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
     return CGFLOAT_MIN;
}

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

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

Swift 5

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

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

-1

尝试使用这个:

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

如评论中所提到的,尝试使用以下代码:

 self.tableView.rowHeight = 0;

1
不要使用这个,而是调用 self.tableView.rowHeight = 0; - rmaddy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接