【问题标题】:iPhone UITableView : How to remove the spacing between sections in group style table?iPhone UITableView:如何删除组样式表中部分之间的间距?
【发布时间】:2014-03-13 08:51:50
【问题描述】:

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

这是输出的屏幕截图:

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

【问题讨论】:

  • 您想要 10 个部分标题和 no 个单元格吗?用 10 个看起来像节标题的单元格做一个表格视图怎么样?
  • 我同意迈克尔的观点,你应该只使用单元格而不是标题
  • @maddy 我知道使用 10 个单元格可以解决它,但正如我所提到的,我使用标题视图的原因有点复杂,我无法解释。我创建了一种可扩展的单元格视图。同样,它不完全像可扩展的,但有很多其他限制。

标签: ios iphone objective-c uitableview tableview


【解决方案1】:

试试这个..

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

也没有表格分隔符。

【讨论】:

  • 是的,它正在工作,我刚刚从 heightForFooterInSection 方法返回 0.01f 并且它工作了。太感谢了 。如果您可以编辑答案并使其可能为 0.01,我可以将其标记为正确答案。
  • @RAJA- 非常感谢。这真是一个聪明的答案,我只是没有看。
  • 为什么不能是0?
【解决方案2】:

如果您在 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
}

【讨论】:

  • 太棒了,它工作得很好,我在标题中也遇到了问题,它也在那里工作。
【解决方案3】:

试试下面的代码

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

你也可以按照委托方法进行管理

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

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

【讨论】:

  • 没有页脚,因此使用该代理不会改变任何内容。
  • 我尝试使用heightForFooterInSection,它在分组表视图中为我工作
  • 您是否也实现了titleForFooterInSectionviewForFooterInSectionheightForFooterInSection 应该只有在其他两个也实现的情况下才会产生影响。
  • @rmaddy - 不,在我的项目中我不使用两种方法 (titleForFooterInSection 或 viewForFooterInSection) .. 然后也为我工作。
  • 有趣。我刚刚尝试并实施heightForFooterInSection 确实对组部分之间的间距有一些影响,但您不能完全删除空间。
【解决方案4】:

分组样式表视图有一个默认页脚,您还应该自定义页脚以覆盖它

试试朴素的风格。

typedef enum {
   UITableViewStylePlain,
   UITableViewStyleGrouped
} UITableViewStyle;

【讨论】:

    【解决方案5】:

    您可以直接在界面中执行此操作,您必须像这样将页脚高度设置为 1:

    【讨论】:

      【解决方案6】:

      将表格视图样式从 Grouped 更改为 Plain。这解决了我的问题。

      【讨论】:

        【解决方案7】:

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

        其实这种方式的正确答案

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

        【讨论】:

          【解决方案8】:

          您需要使用方法 heightForHeaderInSection 。您也可以根据不同的部分进行更改,例如。在某些部分,您可能需要显示更多距离,而在某些部分,您不想显示差距。对于这种情况,您可以使用 0.000001f 的 CGFLOAT_MIN。举个例子,如何使用不同标题高度的不同部分

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

          希望对你有所帮助。

          【讨论】:

            【解决方案9】:

            试试这个:

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

            如 cmets 中所述,尝试使用这个:

             self.tableView.rowHeight = 0;
            

            【讨论】:

            • 而不是这个,只需调用self.tableView.rowHeight = 0;
            猜你喜欢
            • 2017-04-17
            • 1970-01-01
            • 2023-04-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-02-26
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多